The function array_shift()
takes one parameter by reference. Passing an array literal causes a fatal error:
$ 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
This fails as expected. However, PHP behaves strangely when the function is called with 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";
When executed:
$ 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
It's understandable that call_user_func_array()
wouldn't trigger a fatal error, but why does the first form work fine?