Press "Enter" to skip to content

ReflectionFunction(Method)引用参数导致Invocation failed

今天同事反馈一个问题, PHP5.2.x在使用反射做函数包装的时候, 得到"Invocation failed"的异常, 而使用call_user_func代替则不会,
原逻辑太复杂, 经过精简以后可重现异常的代码如下(使用ReflectionFunction为例, ReflectionMethod类似):

function who(&$name) {
    echo $name;
}
$name = "laruence";
$method = new ReflectionFunction("who");
$method->invokeArgs(array($name));
//异常:
Uncaught exception 'ReflectionException' with message
'Invocation of function who() failed'

Filed in PHP应用, PHP源码分析
with 8 Comments

深入理解PHP之匿名函数

PHP中, 传递Callback的方式, 一直很丑陋. 在PHP5.3以前, 我们只有俩种选择:

1. 字符串的函数名
2. 使用create_function的返回值

在PHP5.3以后, 我们多了一个选择, 也就是Closure,

$func = function () { ... };
array_walk($arr, $func);

Filed in PHP应用
with 15 Comments