函数array_shift()通过引用获取一个参数.传递数组文字会导致致命错误:
$php -r ‘var_export(array_shift(array(“Test #0”));’;echo
Fatal error: Only variables can be passed by reference in Command line code on line 1
这按预期失败.但是,当使用call_user_func_array调用函数时,PHP表现得很奇怪:
var_export(call_user_func_array("array_shift", array(array("Test #1"))));
echo "\n";
$arg1 = array("Test #2");
var_export(call_user_func_array("array_shift", array($arg1)));
echo "\n";
$args = array(array("Test #3"));
var_export(call_user_func_array("array_shift", $args));
echo "\n";
执行时:
$php test.php
‘Test #1’
Warning: Parameter 1 to array_shift() expected to be a reference, value given in /Users/kcc/test.php on line 6
NULL
Warning: Parameter 1 to array_shift() expected to be a reference, value given in /Users/kcc/test.php on line 10
NULL
可以理解的是,call_user_func_array()不会触发致命错误,但为什么第一个表单工作正常?