views:

592

answers:

2

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 );
+3  A: 

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.

Steven Oxley
A: 

Except you are using deprecated functionality here. You'll generate a warning in PHP5 making it less than perfect.

Warning: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of runtime function name. If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file in ...

Unfortunately, there doesn't appear to be any other option as far as I can discover.

voidstate
Steven Oxley