Re: [PHP3] Question about $x->prop->prop syntax

From: Date: Thu, 10 Feb 2000 12:38:12 +0000
Subject: Re: [PHP3] Question about $x->prop->prop syntax
References: 1 2  Groups: php.version4 
Request: Send a blank email to php-version4+get-10018@lists.php.net to get a copy of this message
At 11:20 AM +0000 2000-02-10, kev wrote:
I think this one must get everyone at some point :) IIRC you _must_ use temporary variable approach. Like you, I found this out myself, but I have read it somewhere since. I've just had a quick scan of the PHP manual (Classes and Objects section) but couldn't see it in there. Most likely, it was on a tutorial at http://www.phpbuilder.com. Happy hunting! - Kev
Leon Atkinson mentions this in Core PHP Programming in relation to PHP 3. I guess I was hoping that this limitation would have gone away in PHP 4, but it seems not to have. Is this something likely to remain true as work on PHP 4 continues?
Paul DuBois wrote:
I have the following simple class definitions: class C1 {
         var     $prop1;
} class C2 {
         var     $prop2;
         function f ()
         {
                 return ($this->prop2);
         }
} C1 is a child class, C2 is a parent class, and I assign a child instance to the $prop2 property of the C2 parent class:
 $c1 = new C1;           # create new child
$c1->prop1 = "prop1"; # assign its property a value
 $c2 = new C2;           # create new parent
 $c2->prop2 = $c1;       # assign the child to its property
My question has to do with what kinds of syntax I can expect to work when I want to access the child property $prop1 through the parent. This works: $str = $c2->prop2->prop1; This does not appear to work (parse error): $str = $c2->f()->prop1; If I use an intermediate variable, I can make it work: $tmp = $c2->f(); $str = $tmp->prop1; Should I expect $c2->f()->prop1 to work, or is the PHP parser simply not equipped to handle this kind of syntactical construction? Or am I not using the correct syntax?
I'm using PHP 3 and PHP 4.0b3.
-- Paul DuBois, paul@snake.net

« previous php.version4 (#10018) next »