[英]PHP: call_user_func_array: pass by reference issue
The below function generates error when a function contains referenced arguments eg: 当函数包含引用的参数时,以下函数会生成错误,例如:
function test(&$arg, &$arg2)
{
// some code
}
Now I can not use call_user_func_array
for above function, it will generate an error. 现在我不能将
call_user_func_array
用于上面的函数,它会产生错误。
How to solve this problem? 如何解决这个问题呢?
I do need to use call_user_func_array
. 我确实需要使用
call_user_func_array
。
Also assume that i don't know beforehand whether they are passed by reference or passed by value. 还假设我事先不知道它们是通过引用传递还是通过值传递。
Thanks 谢谢
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(¶m1, ¶m2));
A great workaround was posted on http://www.php.net/manual/de/function.call-user-func-array.php#91503 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. 实际的黑客被评论所包围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.