Chaining Class Method Calls (aka Ganging)

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


I am having problems chaining functions which return objects like this

echo $thisDog->owner()->name();


echo $thisDog->owner()->legs;

I get the following error:  unexpected T_OBJECT_OPERATOR, expecting ',' or

The example code more or less sums up what I am up to internally.  Any
suggestions as to where my syntax is wrong?

I have verified via print_r() that I have the objects I think I do being



class Dog extends Mammal {
   function owner() {
          $ownerObject = new Owner;
          $owner =    $ownerObject ->yatayata ();   // this is a db record
which is objectified into a bona fide Owner object
          return $owner;

 class Owner extends Mammal {
    var $legs = 2;
   function name() {
    return $whatever;

 $dogObj = new Dog;
 $thisDog = $dpgObject->retrieve("id = '1'");
 echo $thisDog->owner()->name();

Re: Chaining Class Method Calls (aka Ganging)

On Wed, 2 Mar 2005 13:37:48 -0600, "jerrygarciuh"

Quoted text here. Click to load it

 Unfortunately, that only works in PHP 5. PHP 4 chokes with the errors you

 In PHP 4 you have to (a) use a temporary and (b) make sure you don't trip up
on PHP 4's nasty assign-object-by-copy semantics; assign by reference:

$owner =& $thisDog->owner();
echo $owner->name();

< Space: disk usage analysis tool

Re: Chaining Class Method Calls (aka Ganging)

Thanks for the reply Andy!  Oh well, I guess I will just have to wait
until 5 is available on my box.  30+ sites is too many to move.



Site Timeline