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 );
The call_user_func() is an inbuilt function in PHP which is used to call the callback given by the first parameter and passes the remaining parameters as argument. It is used to call the user-defined functions.
To pass a value by reference, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to, by their arguments.
Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in.
Pass by value means that a copy of the actual parameter's value is made in memory, i.e. the caller and callee have two independent variables with the same value. If the callee modifies the parameter value, the effect is not visible to the caller. Overview: Passes an argument by value.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With