Bluehost.com Web Hosting $6.95

Problem with call_user_func_array and passing an array of objects

This is a discussion on Problem with call_user_func_array and passing an array of objects within the PHP General forums, part of the PHP Programming Forums category; Hi, I wish to call function f($params) using call_user_func_array() and pass to f() an array of two objects as ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-03-2004
Jon Goodwin
 
Posts: n/a
Default Problem with call_user_func_array and passing an array of objects

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=
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:57 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0