Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ); 
like image 608
Steven Oxley Avatar asked Nov 17 '08 06:11

Steven Oxley


People also ask

What is the use of Call_user_func_array?

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.

How to pass value by reference?

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.

What are passing parameters by reference used for?

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.

Which is true about pass by value parameter?

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.


1 Answers

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.

like image 163
Steven Oxley Avatar answered Sep 22 '22 01:09

Steven Oxley