Alain
Alain

Reputation: 37004

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?

Upvotes: 25

Views: 8008

Answers (1)

Alain
Alain

Reputation: 37004

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));

Upvotes: 44

Related Questions