PHP: call_user_func_array: pass by reference issue

爷,独闯天下 提交于 2019-11-28 13:40:08

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.

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!