Chaining Class Method Calls (aka Ganging)

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-02-2005
jerrygarciuh
 
Posts: n/a
Default Chaining Class Method Calls (aka Ganging)

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();


Reply With Quote
  #2 (permalink)  
Old 03-02-2005
Andy Hassall
 
Posts: n/a
Default Re: Chaining Class Method Calls (aka Ganging)

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
Reply With Quote
  #3 (permalink)  
Old 03-02-2005
jerrygarciuh
 
Posts: n/a
Default 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.

Sigh,

jg

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 10:40 AM.


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