phpreferencecallback

Is it possible to pass parameters by reference using call_user_func_array()?


When using call_user_func_array() I want to pass a parameter by reference. How would I do this. For example

function toBeCalled( &$parameter ) {
    //...Do Something...
}

$changingVar = 'passThis';
$parameters = array( $changingVar );
call_user_func_array( 'toBeCalled', $parameters );

Solution

  • To pass by reference using call_user_func_array(), the parameter in the array must be a reference - it does not depend on the function definition whether or not it is passed by reference. For example, this would work:

    function toBeCalled( &$parameter ) {
        //...Do Something...
    }
    
    $changingVar = 'passThis';
    $parameters = array( &$changingVar );
    call_user_func_array( 'toBeCalled', $parameters );
    

    See the notes on the call_user_func_array() function documentation for more information.