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?

ekremkaraca
  • 1,453
  • 2
  • 18
  • 37
d3cima
  • 729
  • 1
  • 10
  • 31
  • 1
    possible duplicate of [Event-driven architecture and hooks in PHP](http://stackoverflow.com/questions/6846118/event-driven-architecture-and-hooks-in-php) – Prasanth Jun 08 '14 at 18:24
  • 1
    very good question though – Prasanth Jun 08 '14 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 – d3cima Jun 08 '14 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. – monocell Jun 08 '14 at 21:26
  • 1
    this may be useful: [1905800/php-call-user-func-array-pass-by-reference-issue](http://stackoverflow.com/questions/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](http://www.php.net/manual/de/function.call-user-func-array.php#91503) – Ryan Vincent Jun 08 '14 at 21:28
  • Actually, my code has the example taken from the manual. But it seems the last PHP version (5.5) doesn't allow this king of manipulations. I also tried my code with the ReflectionMethod::invokeArgs() method. – d3cima Jun 09 '14 at 08:08

0 Answers0