1

I've looked at Is it possible to pass parameters by reference using call_user_func_array()?, and http://php.net/manual/en/function.call-user-func-array.php. The approved technique for passing by reference using call_user_func_array() seems to be by making the parameter array an array of variable references. For example, setting $parameters = array( &$some_variable). My question is, can we instead make the parameter array an array of variables (not references), and pass the whole parameter array as a reference instead? This is illustrated below:

function toBeCalled( &$parameter1, $parameter2 ) {
    //...Do Something...
}

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

Notice that the function toBeCalled expects the first variable as a reference, and the second as a value. The reason I ask is because the syntax here is convenient, and it seems to work (see this PHP 5.3 patch for the DruTex module for Drupal - http://drupal.org/node/730940#comment-4054054), but I'm just checking what experts think about it.

Community
  • 1
  • 1
Shaun Dychko
  • 825
  • 1
  • 9
  • 11
  • Use the first version, since it works. The latter doesn't even with PHP 5.3 (or did your example work for you?), and will be disallowed from PHP 5.4 anyway (Zend memleaks for forcibly passed references). – mario Jan 14 '12 at 18:24

1 Answers1

0

You can't, because the moment you put things into an array not by reference, they are copied. So no matter what you do with the array afterwards, it will not affect the original variables you put in (if they were even variables to start with).

The thing you linked to doesn't seem to be relevant. It is removing a pointless call-time pass-by-reference.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • Thanks @newacct, your point about things being copied into arrays helps make it clear why call_user_func_array needs to have elements that are references (which at first glance seems to violate the restriction against call-time pass-by-reference). After testing: `' . $x1); echo('
    ' . $x2); ?>` returns errors.
    – Shaun Dychko Jan 16 '12 at 19:39