Consider the following example :
function myTest(&$var)
{
$var++;
echo "var = {$var}\n";
}
$x = 42;
call_user_func('myTest', $x);
It shows the warning :
Warning: Parameter 1 to myTest() expected to be a reference, value given in /home/alain/workspace/echo/echo.php(57) : eval()'d code on line 7
Note: code written on an online sandbox, explaning the eval.
Any idea how I can pass reference to call_user_func
family functions?
I found my answer on the PHP manual :
Note:
Note that the parameters for call_user_func() are not passed by reference.
They also give a trick to do the job :
$x = 42;
call_user_func_array('myTest', array(&$x));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With