PHP code example of nickjbedford / hooks

1. Go to this page and download the library: Download nickjbedford/hooks library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nickjbedford / hooks example snippets


$menu = [
    'Plugins' => [
        new MenuItem('Plugin Manager', '/plugins/index')
    ]
];
$menuItems = hook_filter('main_menu', $menu);

// Add menu items to UI...

hook_add('main_menu', function(array $menu)
{
    $menu['Plugins'][] = new MenuItem('My Plugin', '/my-plugin/');
    return $menu;
});

$hook = \YetAnother\Hook::get('do_stuff');
$hook->add(function($param)
{
    print($param);
});
$hook->run('Hello, world!');

// "Hello, world!" is printed

hook_add('do_stuff', fn($param) => print($param));
hook_run('do_stuff', 'Hello, world!');

hook_add('accumulate', fn($initial, $parameter) => $initial + $parameter);
hook_add('accumulate', fn($initial, $parameter) => $initial * $parameter);

$result = hook_filter('accumulate', 5, 7);

// $result = 54 ((5 + 7) * 7)

hook_add('menu', function(array $menu)
{
    $menu[] = 'World';
    return $menu;
});

$menu = hook_filter('menu', [ 'Hello' ]);

// $menu = [ 'Hello', 'World' ]