Home > Blockchain >  PHP-Hooks: Different result on multiple hooks
PHP-Hooks: Different result on multiple hooks

Time:02-02

I'm using this library: https://github.com/bainternet/PHP-Hooks and seeking if anyone familiar with this library to look on my issue.

This is my code:

if (!function_exists('app_hooks')) {

    function app_hooks() {
        require_once("php-hooks.php");
        global $hooks;
        return $hooks;
    }

}

app_hooks()->add_filter('my_filter', function ($value) {
    echo $value; //output: some value to be passed
    return "x";
});
app_hooks()->add_filter('my_filter', function ($value) {
    echo $value; //output: xy (but should be 'some value to be passed')
    return "y";
});

$my_tabs = array();
$my_tabs[] = app_hooks()->apply_filters('my_filter', 'some value to be passed');

echo "<pre>";
print_r($my_tabs);
exit;

The $my_tabs is giving this output:

Array
(
    [0] => y
)

But I need both values like this:

Array
(
    [0] => x,
    [1] => y
)

Can anyone please inform me where am I wrong or if this is possible. Thanks in advance.

CodePudding user response:

Applying a single hook will only push one value onto the array. You need to push in a loop to get multiple values, so I suggest giving different names to your hooks.

app_hooks()->add_filter('my_filter1', function ($value) {
    echo $value; //output: some value to be passed
    return "x";
});
app_hooks()->add_filter('my_filter2', function ($value) {
    echo $value; //output: xy (but should be 'some value to be passed')
    return "y";
});
$filters = ['my_filter1', 'my_filter2'];
foreach ($filters as $f) {
    $my_tabs[] = app_hooks()->apply_filters($f, 'some value to be passed');
}
  •  Tags:  
  • Related