Hooks::dispatch( string $hook, array $parameters = array() ): boolean

In this article

Dispatch a message

Parameters

$hookstringrequired
Hook name
$parametersarrayoptional
Parameters to pass to callbacks

Default:array()

Return

boolean Successfulness

Source

public function dispatch($hook, $parameters = []) {
	if (is_string($hook) === false) {
		throw InvalidArgument::create(1, '$hook', 'string', gettype($hook));
	}

	// Check strictly against array, as Array* objects don't work in combination with `call_user_func_array()`.
	if (is_array($parameters) === false) {
		throw InvalidArgument::create(2, '$parameters', 'array', gettype($parameters));
	}

	if (empty($this->hooks[$hook])) {
		return false;
	}

	if (!empty($parameters)) {
		// Strip potential keys from the array to prevent them being interpreted as parameter names in PHP 8.0.
		$parameters = array_values($parameters);
	}

	ksort($this->hooks[$hook]);

	foreach ($this->hooks[$hook] as $priority => $hooked) {
		foreach ($hooked as $callback) {
			$callback(...$parameters);
		}
	}

	return true;
}

User Contributed Notes

You must log in before being able to contribute a note or feedback.