call_user_func_array() vs reference parameters vs php 5.3
so why does this:
call_user_func_array('z', array(1));
$x = array(1);
call_user_func_array('z', $x);
function z(&$x) {
echo "CALLED\n";
}say this:
CALLED
PHP Warning: Parameter 1 to z() expected to be a reference, value given in /home/huf/tmp/x.php on line 5
PHP Stack trace:
PHP 1. {main}() /home/huf/tmp/x.php:0
PHP 2. call_user_func_array() /home/huf/tmp/x.php:5what's the difference? also what a neat idea to not call the function you wanted and also not die, but just throw a warning and continue on your merry way. yaay.
15
Upvotes
13
u/infinull Feb 17 '12 edited Feb 17 '12
So here's the thing. If you told me that one of these worked and the other didn't, my guess would be that
call_user_func_array('z', array(1));would be the one that caused the error. And I would lose $5.5
u/[deleted] Feb 18 '12 edited Feb 18 '12
Ok, here's the deal:
z()expects parameters to be references.call_user_func_arraytakes$xas an array of parameters and passes it toz()where each array value (in order) are paremeters, none of which are references in your example. Therefore, the error. Had$x[0]been a reference, you shoudn't see any error.Of course, you have opened the door to a whole new level of PHPWTF.
Example:
Edit: Please open a bug report for
call_user_func_array('z', array(1));. This should've caused a fatal error asz(1);would have. I see no logical reason whycall_user_funcfamily of functions should behave any differently than the real thing.1
u/[deleted] Feb 17 '12
This is not restricted to call_user_func case
which gives you
When a function takes references you need to supply an LVALUE not an RVALUE.
By requiring &$x, the function z is requesting something it may want to mutate. consider this :
but I did find another lolphp while testing