PHP: call_user_func_array: pass by reference issue

后端 未结 2 993
情歌与酒
情歌与酒 2020-12-11 18:35

The below function generates error when a function contains referenced arguments eg:

function test(&$arg, &$arg2)
{
  // some code
}
<
相关标签:
2条回答
  • 2020-12-11 19:04

    A great workaround was posted on http://www.php.net/manual/de/function.call-user-func-array.php#91503

    function executeHook($name, $type='hooks'){ 
        $args = func_get_args(); 
        array_shift($args); 
        array_shift($args); 
        //Rather stupid Hack for the call_user_func_array(); 
        $Args = array(); 
        foreach($args as $k => &$arg){ 
            $Args[$k] = &$arg; 
        } 
        //End Hack 
        $hooks = &$this->$type; 
        if(!isset($hooks[$name])) return false; 
        $hook = $hooks[$name]; 
        call_user_func_array($hook, $Args); 
    } 
    

    The actual hack is surrounded by comments.

    0 讨论(0)
  • 2020-12-11 19:10

    When storing your parameters in the array, make sure you are storing a reference to those parameters, it should work fine.

    Ie:

    call_user_func_array("test", array(&param1, &param2));
    
    0 讨论(0)
提交回复
热议问题