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.
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.
Passing parameters with call_user_func?
Use call_user_func_array, you can supply a list of parameters as array.
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.
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));
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).
Pass variable number of params without call_user_func_array()
As commented in the thread question post's comments this is an example and not necessarily (likely) best practice.
//Some vars
$foo = "shoe";
$bar = "bucket";
//Array of references
$arr = Array(&$foo, &$bar);
//Show that changing variable value affects array content
$foo = "water";
echo $arr[0];
//Sample function
function fooBar($a)
{
$a[0] = "fire";
}
//Call sample function
call_user_func("fooBar",$arr);
//Show that function changes both array contents and variable value by reference
echo $arr[0];
echo $foo;
Expanding a bit on the discussion, again not the most industry standard approach but it'll do the job.
function pushRefOnArray(&$arr, &$var, $key = false)
{
if(isset($key))
$arr[$key] = &$var;
else
$arr[] = &$var;
}
Essentially you can dynamically build your array and call pushRefToArray() any time you need to pass an item to be passed as reference rather than by value.
call_user_func_array passing arguments to a constructor
You can't call the constructor of $class
like this:
call_user_func_array (new $class, $args);
That's no valid callback as first parameter. Let's pick this apart:
call_user_func_array (new $class, $args);
Is the same as
$obj = new $class;
call_user_func_array ($obj, $args);
As you can see, the constructor of $class
has been already called before call_user_func_array
comes into action. As it has no parameters, you see this error message:
Missing argument 1 for Product::__construct()
Next to that, $obj
is of type object. A valid callback must be either a string or an array (or exceptionally a very special object: Closure
, but that's out of discussion here, I only name it for completeness).
As $obj
is an object and not a valid callback, so you see the PHP error message:
Object of class Product could not be converted to string.
PHP tries to convert the object to string, which it does not allow.
So as you can see, you can't easily create a callback for a constructor, as the object yet not exists. Perhaps that's why you were not able to look it up in the manual easily.
Constructors need some special dealing here: If you need to pass variable arguments to a class constructor of a not-yet initialize object, you can use the ReflectionClass
to do this:
$ref = new ReflectionClass($class);
$new = $ref->newInstanceArgs($args);
See ReflectionClass::newInstanceArgs
How to call_user_func_array on an object that has been set to a variable?
In the docs:
// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
Example:
class Model
{
public $model;
public function __call($name, $params = [])
{
call_user_func_array([$this->model, $name], $params);
}
}
class User
{
public function first($one, $two)
{
echo $one, $two;
}
}
$example = new Example();
$example->model = new User();
$example->first('one', 'two');
Related Topics
MySQL - Access Denied for User
How to Keep JSON_Encode() from Dropping Strings with Invalid Characters
How to Get the Newest File in a Directory in PHP
Which Line Break in PHP Mail Header, \R\N or \N
Should I Use Prepared Statements for MySQL in PHP Performance-Wise
Improve Password Hashing with a Random Salt
Php: Get N-Th Item of an Associative Array
How to Pass Parameters by Reference Using Call_User_Func_Array()
PHP Composer Behind Http Proxy
Flatten Multidimensional Array Concatenating Keys
How to Properly Use While Loop in Pdo Fetchall
Accessing Variables and Methods Outside of Class Definitions
Find Number Which Is Greater Than or Equal to N in an Array
Getting Pear to Work on Xampp (Apache/MySQL Stack on Windows)
Insert Data Through Ajax into MySQL Database