Hook::set()

Sets hooks by its container name.

Table of Contents
  1. Description
  2. Example

Description

Hook::set(array|string $name, ?callable $fn = null, float $stack = 10): mixed;

This method sets a hook to a container. The hooks in the container can then be executed using the Hook::fire() method at the place you want. The $stack parameter is optional and defaults to 10 if not set. This parameter serves to sort the hook execution priority. The smaller the value, the earlier the hook will be executed. Its value does not have to be an integer, you can also use a fractional value to insert a new hook between two narrow hook execution priorities.

Example

// Replace `'\n'` with `'<br>'`
Hook::set('content', static function ($content) {
    return '<p>' . strtr($content, ["\n" => '<br>']) . '</p>';
}, 0);

// Add `<article>` wrapper
Hook::set('content', static function ($content) {
    return '<article>' . $content . '</article>';
}, 10);

// Add `<div>` wrapper
Hook::set('content', static function ($content) {
    return '<div>' . $content . '</div>';
}, 9);

// Add `<section>` wrapper
Hook::set('content', static function ($content) {
    return '<section>' . $content . '</section>';
}, 9.5);

// This returns `'<article><section><div><p>aaa<br>aaa<br>aaa</p></div></section></article>'`
$content = Hook::fire('content', ["aaa\naaa\naaa"]);

Hook::is()

Checks the hook name that is being executed.

Hook::set()

Sets hooks by its container name.