1

Here's main application:

$test=array();
invoke_hooks("hook",$test);

Two functions of hook:

function first_hook(&$a = array()) {
    $a[40] = "1";
}

function second_hook(&$a = array()) {
    $a[300] = "2";
}

function invoke_hooks($hook_name) {
    $args = func_get_args();
    array_shift($args);
    $functions = get_defined_functions();
    foreach ($functions as $f) {
        if (substr($f, -strlen($hook_name)) === $hook_name) {
            call_user_func_array($f, &$args);
        }
    }
}

The idea is to get :

$test[40]=="1";
$test[300]=="2";

How can I make it?

6
  • 1
    possible duplicate of Event-driven architecture and hooks in PHP Commented Jun 8, 2014 at 18:24
  • 1
    very good question though Commented Jun 8, 2014 at 18:24
  • I'm sure the purpose is the same, but the way to get it working is not the same. I already searched on this post :D Commented Jun 8, 2014 at 19:47
  • A second (unrelated) issue is that get_defined_functions separates user defined and internal functions, so you probably want something like foreach($functions['user'] as $f) { on the relevant line. Commented Jun 8, 2014 at 21:26
  • 1
    this may be useful: 1905800/php-call-user-func-array-pass-by-reference-issue. see the second answer. It references this exanple in the manual: function.call-user-func-array.php#9150 Commented Jun 8, 2014 at 21:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.