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

后端 未结 5 1280
天命终不由人
天命终不由人 2021-01-17 07:06

It fires out when I try to call function with argument by reference

function test(&$a) ...

through

call_user_func(\'tes         


        
相关标签:
5条回答
  • 2021-01-17 07:49

    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.

    0 讨论(0)
  • 2021-01-17 08:06

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

    0 讨论(0)
  • 2021-01-17 08:08

    You might consider the closure concept with a reference variable tucked into the "use" declaration. For example:

    $note = 'before';
    $cbl = function( $msg ) use ( &$note )
    {
        echo "Inside callable with $note and $msg\n";
        $note = "$msg has been noted";
    };
    call_user_func( $cbl, 'after' );
    echo "$note\n";
    

    Bit of a workaround for your original problem but if you have a function that needs call by reference, you can wrap a callable closure around it, and then execute the closure via call_user_func().

    0 讨论(0)
  • 2021-01-17 08:09

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

    0 讨论(0)
  • 2021-01-17 08:10

    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.

    0 讨论(0)
提交回复
热议问题