Problem with call_user_func_array and passing an array of objects
| From: | Jon Goodwin | Date: | Sat, 03 Jan 2004 13:29:08 +0000 |
| Subject: | Problem with call_user_func_array and passing an array of objects | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-173681@lists.php.net to get a copy of this message | ||
Hi,
I wish to call function f($params) using call_user_func_array() and pass to
f() an array of two objects as a parameter.
When I call f() directly, it works as expected. Function f() recognises that
2 objects are in the array and I can access their member variables.
When I call the same function via call_user_func_array(), function f()
recognises only the first object. Also, the same code that I used to access
the objects when f() was called directly does not work when f() is called
via call_user_func_array().
I have included some sample code below to illustrate the problem.
I am running PHP version 4.2.2 on Windows NT 5.0 and Apache 2.0.39.
I also have the same problem when I run PHP 4.3.0 on Linux and Apache 1.3.27
Thanks in advance for your help.
<?
class tst
{
var $v1;
var $v2;
function tst($a, $b)
{
$this->v1 = $a;
$this->v2 = $b;
}
}
$tst1 = new tst("a", "b");
$tst2 = new tst("d", "e");
$arr = array($tst1, $tst2);
echo("<B>Call function f() as regular function</B><BR>");
f($arr);
echo("<B>Calling function f() via call_user_func_array()</B><BR>");
call_user_func_array(f, $arr);
function f($params)
{
var_dump($params);
echo("Count=" . count($params). "<BR>");
echo("Param[0]->v1=" . $params[0]->v1 . " Param[0]->v2=" .
$params[0]->v2
. "<BR>");
echo("Param[1]->v1=" . $params[1]->v1 . " Param[1]->v2=" .
$params[1]->v2
. "<BR>");
echo("<P>");
}
?>
OUTPUT >>>>>
Call function f() as regular function
array(2) { [0]=> object(tst)(2) { ["v1"]=> string(1) "a"
["v2"]=> string(1)
"b" } [1]=> object(tst)(2) { ["v1"]=> string(1) "d"
["v2"]=> string(1)
"e" } } Count=2
Param[0]->v1=a Param[0]->v2=b
Param[1]->v1=d Param[1]->v2=e
Calling function f() via call_user_func_array()
object(tst)(2) { ["v1"]=> string(1) "a" ["v2"]=> string(1)
"b" } Count=1
Param[0]->v1= Param[0]->v2=
Param[1]->v1= Param[1]->v2=