MilkCore

Hooks
in package

Hook management system that allows registering one or more functions to be called later

Tags
example
class test {
   public function __construct() {
       HOOKS::set('my_hook', [get_called_class(), 'test']);
   }

   public static function test($a, $b) {
       print "test ".$a." ".$b."\n";
       return $a + $B;
   }
}
function test_new($a) {
    print "Somma di A ".$a."\n";
}

new test();
HOOKS::set('my_hook', 'test_new');
HOOKS::run('my_hook', 1, 2);

Table of Contents

Methods

get_hook_registrations()  : array<string|int, mixed>
Retrieves all registered functions for a specific hook, sorted by order.
run()  : mixed
Calls a group of registered functions, modifying and returning the first value (if present)
set()  : void
Registers a function for a specific hook

Methods

get_hook_registrations()

Retrieves all registered functions for a specific hook, sorted by order.

public static get_hook_registrations(string $name) : array<string|int, mixed>
Parameters
$name : string

The name of the hook.

Return values
array<string|int, mixed>

An array of registered function metadata (['fn' => callable, 'order' => int, 'caller' => string]), or an empty array if the hook has no registrations.

run()

Calls a group of registered functions, modifying and returning the first value (if present)

public static run(string $name, mixed ...$args) : mixed

When a hook is called, arguments are passed. The first argument will be the return value

Parameters
$name : string

The name of the hook to run

$args : mixed

Arguments to pass to the hook functions

Tags
example
$return = Hooks::run($name, ...$args)
Return values
mixed

The modified value after all hooks have processed it

set()

Registers a function for a specific hook

public static set(string $name, callable $function[, int $order = 20 ]) : void
Parameters
$name : string

The name of the hook

$function : callable

The function to be called

$order : int = 20

The execution order (lower numbers execute first)

Tags
example
Hooks::set($name, $function, $order = int)

        
On this page

Search results