This is a discussion on Chaining Class Method Calls (aka Ganging) within the PHP Language forums, part of the PHP Programming Forums category; Hello, I am having problems chaining functions which return objects like this example: echo $thisDog->owner()->name(); or ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello,
I am having problems chaining functions which return objects like this example: echo $thisDog->owner()->name(); or 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 returned. TIA jg 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(); |
|
|||
|
On Wed, 2 Mar 2005 13:37:48 -0600, "jerrygarciuh"
<designs@no.spam.nolaflash.com> wrote: >I am having problems chaining functions which return objects like this >example: > >echo $thisDog->owner()->name(); Unfortunately, that only works in PHP 5. PHP 4 chokes with the errors you posted. 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(); -- Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool |