Why Does PHP's Call_User_Func() Function Not Support Passing by Reference

Why does PHP's call_user_func() function not support passing by reference?

The answer is embedded deep down in the way references work in PHP's model - not necessarily the implementation, because that can vary a lot, particularly in the 5.x versions. I'm sure you've heard the lines, they're not like C pointers, or C++ references, etc etc... Basically when a variable is assigned or bound, it can happen in two ways - either by value (in which case the new variable is bound to a new 'box' containing a copy of the old value), or by reference (in which case the new variable is bound to the same value box as the old value). This is true whether we're talking about variables, or function arguments, or cells in arrays.

Things start to get a bit hairy when you start passing references into functions - obviously the intent is to be able to modify the original variables. Quite some time ago, call-time pass-by-reference (the ability to pass a reference into a function that wasn't expecting one) got deprecated, because a function that wasn't aware it was dealing with a reference might 'accidentally' modify the input. Taking it to another level, if that function calls a second function, that itself wasn't expecting a reference... then everything ends up getting disconnected. It might work, but it's not guaranteed, and may break in some PHP version.

This is where call_user_func() comes in. Suppose you pass a reference into it (and get the associated the call-time pass-by-reference warning). Then your reference gets bound to a new variable - the parameters of call_user_func() itself. Then when your target function is called, its parameters are not bound where you expect. They're not bound to the original parameters at all. They're bound to the local variables that are in the call_user_func() declaration. call_user_func_array() requires caution too. Putting a reference in an array cell could be trouble - since PHP passes that array with "copy-on-write" semantics, you can't be sure if the array won't get modified underneath you, and the copy won't get detached from the original reference.

The most insightful explanation I've seen (which helped me get my head around references) was in a comment on the PHP 'passing by reference' manual:

http://ca.php.net/manual/en/language.references.pass.php#99549

Basically the logic goes like this. How would you write your own version of call_user_func() ? - and then explain how that breaks with references, and how it fails when you avoid call-time pass-by-reference. In other words, the right way to call functions (specify the value, and let PHP decide from the function declaration whether to pass value or reference) isn't going to work when you use call_user_func() - you're calling two functions deep, the first by value, and the second by reference to the values in the first.

Get your head around this, and you'll have a much deeper understanding of PHP references (and a much greater motivation to steer clear if you can).

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

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.

Objects are passed by reference. Parameters to call_user_func aren't. What gives?

Parameters passing

The main issue is - that parameters, passed to call_user_func() will be passed as values - so they will be copy of actual data. This behavior overrides the fact, that

objects are passed by reference. Note:

Note that the parameters for call_user_func() are not passed by
reference.

Tracking error

You're not fully correct about "silent agreement" in such cases. You will see error with level E_WARNING in such cases:


Warning: Parameter 1 to with_ref() expected to be a reference, value given in

So - you will be able to figure out that you're mixing reference and values passing

Fixing the issue

Fortunately, it's not too hard to avoid this problem. Simply create reference to desired value:

class Example {
function RunEvent($event) {
if (isset($this->events[$event])) {
foreach ($this->events[$event] as $k => $v) {

$obj = &$this;
call_user_func($v, $obj);
}
}
}
}

-then result will be quite as expected:


object(Example)#1 (3) {
["events"]=>
array(1) {
["example"]=>
array(2) {
[0]=>
string(8) "with_ref"
[1]=>
string(11) "without_ref"
}
}
["with_ref"]=>
bool(true)
["without_ref"]=>
bool(true)
}

PHP callback using a reference

Documentation says "Note: Note that the parameters for call_user_func() are not passed by reference."

You might use call_user_func_array instead.

function increment(&$a) {
$a++;
}

$x = 1;

call_user_func_array("increment", array(&$x));

echo $x;

PHP: call_user_func_array: pass by reference issue

A great workaround was posted on http://www.php.net/manual/de/function.call-user-func-array.php#91503

function executeHook($name, $type='hooks'){ 
$args = func_get_args();
array_shift($args);
array_shift($args);
//Rather stupid Hack for the call_user_func_array();
$Args = array();
foreach($args as $k => &$arg){
$Args[$k] = &$arg;
}
//End Hack
$hooks = &$this->$type;
if(!isset($hooks[$name])) return false;
$hook = $hooks[$name];
call_user_func_array($hook, $Args);
}

The actual hack is surrounded by comments.

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

call_user_func can only pass parameters by value, not by reference. If you want to pass by reference, you need to call the function directly, or use call_user_func_array, which accepts references (however this may not work in PHP 5.3 and beyond, depending on what part of the manual look at).

How can I pass reference to call_user_func?

I found my answer on the PHP manual :

Note:

Note that the parameters for call_user_func() are not passed by
reference.

They also give a trick to do the job :

$x = 42;
call_user_func_array('myTest', array(&$x));

Passing a reference using call_user_func_array with variable arguments

There is no way to get a reference out of func_get_args() because it returns an array with a copy of the values passed in. See PHP Reference.

Additionally, since runtime pass by reference is no longer supported, you must denote the reference in each method/function signature. Here is an example that should work around the overall issue of having an Invoker that does pass by reference, but there is no work around for func_get_args().

<?php

class Testme {
public static function foo(&$ref) {
$ref = 1;
}
}

class Invoker {
public static function invoke($func_name, &$args){
call_user_func_array(array('Testme', $func_name), $args);
}
}

$test = 10;

$args[] = &$test;

Invoker::invoke('foo', $args);

var_dump($test);

If you know you want to invoke by reference, this can work for you and perhaps have two invokers, one Invoker::invokeByRef an another normal Invoker::invoke that does the standard invoking by copy.

why pass-by-reference doesn't change the value of a variable?

It's not possible with call_user_func():

From the PHP documentation:

Note that the parameters for call_user_func() are not passed by reference.

That said you still can use call_user_func_array(). Using the reference becomes now possible. Here's the code you want:

function change(&$str)
{
$str='str';
}

$string='string';
$parameters = array(&$string);
call_user_func_array('change',$parameters);
echo $string;

However this solution is now Deprecated.

You still can get rid of call_user_func(), and simply do:

function change(&$str)
{
$str='str';
}

$string='string';
change($string);
echo $string;

Passing parameters with call_user_func?

Use call_user_func_array, you can supply a list of parameters as array.



Related Topics



Leave a reply



Submit