Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass reference to call_user_func?

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?

like image 996
Alain Tiemblo Avatar asked Dec 10 '12 09:12

Alain Tiemblo


1 Answers

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));
like image 164
Alain Tiemblo Avatar answered Oct 22 '22 02:10

Alain Tiemblo