note 47578 added to function.call-user-func-array
| From: | james at gogo dot co dot nz | Date: | Sun, 21 Nov 2004 13:19:29 +0000 |
| Subject: | note 47578 added to function.call-user-func-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-80831@lists.php.net to get a copy of this message | ||
Be aware the call_user_func_array always returns by value, as demonstrated here...
<?php
function &foo(&$a)
{
return $a;
}
$b = 2;
$c =& call_user_func_array('foo', array(&$b));
$c++;
echo $b . ' ' . $c;
?>
outputs "2 3", rather than the expected "3 3".
Here is a function you can use in place of call_user_func_array which returns a reference to the
result of the function call.
<?php
function &ref_call_user_func_array($callable, $args)
{
if(is_scalar($callable))
{
// $callable is the name of a function
$call = $callable;
}
else
{
if(is_object($callable[0]))
{
// $callable is an object and a method name
$call = "\$callable[0]->{$callable[1]}";
}
else
{
// $callable is a class name and a static method
$call = "{$callable[0]}::{$callable[1]}";
}
}
// Note because the keys in $args might be strings
// we do this in a slightly round about way.
$argumentString = array();
$argumentKeys = array_keys($args);
foreach($argumentKeys as $argK)
{
$argumentString[] = "\$args[$argumentKeys[$argK]]";
}
$argumentString = implode($argumentString, ', ');
// Note also that eval doesn't return references, so we
// work around it in this way...
eval("\$result =& {$call}({$argumentString});");
return $result;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.call-user-func-array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47578
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47578&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47578&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47578&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47578&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47578&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47578&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47578&report=yes
Search -- http://master.php.net/manage/user-notes.php