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

后端 未结 5 1352
天命终不由人
天命终不由人 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 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().

提交回复
热议问题