PHP: call_user_func_array: pass by reference issue

后端 未结 2 997
情歌与酒
情歌与酒 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.

提交回复
热议问题