Hook::fire()

Triggers hooks by its container name.

Table of Contents
  1. Description
  2. Example

Description

Hook::fire(array|string $name, array $lot = [], ?object $that = null, ?string $scope = null): mixed;
Hook::fire(array|string $name, array $lot = [], ?string $scope = null): mixed;

Triggers the hooks that have been added to a specific container name. If $that parameter is a class instance, then every time a hook function is executed, in that function there will be a $this context that links to that class instance. The $scope parameter is optional if $that parameter is set, and will default to the scope of that class.

Example

An example of a content hook without context:

Hook::set('content', static function ($content, $type) {
    if ('Markdown' === $type) {
        // <https://michelf.ca/projects/php-markdown>
        return Michelf\Markdown::defaultTransform($content);
    }
    return $content;
}, 1);

$page = (object) [
    'content' => 'Page content goes here.',
    'title' => 'Page Title',
    'type' => 'Markdown'
];

// This returns `'<p>Page content goes here.</p>'` because `$type` value is `'Markdown'`
$content = Hook::fire('content', [$page->content, $page->type]);

An example of a content hook with context:

Hook::set('content', function ($content) {
    if ('Markdown' === $this->type) {
        // <https://michelf.ca/projects/php-markdown>
        return Michelf\Markdown::defaultTransform($content);
    }
    return $content;
}, 1);

$page = (object) [
    'content' => 'Page content goes here.',
    'title' => 'Page Title',
    'type' => 'Markdown'
];

// This returns `'<p>Page content goes here.</p>'` because `$page->type` value is `'Markdown'`
$content = Hook::fire('content', [$page->content], $page);

Hook::fire()

Triggers hooks by its container name.

Hook::is()

Checks the hook name that is being executed.