Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the error "expected to be a reference, value given" appear?

Tags:

php

call_user_func can only pass parameters by value, not by reference. If you want to pass by reference, you need to call the function directly, or use call_user_func_array, which accepts references (however this may not work in PHP 5.3 and beyond, depending on what part of the manual look at).


From the manual for call_user_func()

Note that the parameters for call_user_func() are not passed by reference.

So yea, there is your answer. However, there is a way around it, again reading through the manual

call_user_func_array('test', array(&$b));

Should be able to pass it by reference.


I've just had the same problem, changing (in my case):

$result = call_user_func($this->_eventHandler[$handlerName][$i], $this, $event);

to

$result = call_user_func($this->_eventHandler[$handlerName][$i], &$this, &$event);

seem to work just fine in php 5.3.

It's not even a workaround I think, it's just doing what is told :-)


You need to set the variable equal to the result of the function, like so...

$b = call_user_func('test', $b);

and the function should be written as follows...

function test($a) {
    ...
    return $a
}

The other pass by reference work-a-rounds are deprecated.