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

From: Date: Thu, 10 Feb 2000 15:40:48 +0000
Subject: Re: [PHP4BETA] Re: [PHP3] Question about $x->prop->prop syntax
References: 1 2 3  Groups: php.version4 
Request: Send a blank email to php-version4+get-10029@lists.php.net to get a copy of this message
Paul DuBois wrote: > > 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! You must use an intermmediate var only in PHP3 > 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? did you actually try it? I just did (took less 1'') and seems to work :-> > >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. But , as I undestood it, what you did here is not derivation but delegation, C1 is not a `parent' for C2. > >> This works: > >> > >> $str = $c2->prop2->prop1; > >> > >> This does not appear to work (parse error): > >> > >> $str = $c2->f()->prop1; Yap, but F() does not return any kind of reference or seomthing, and even then this syntax would be just weird. I only sow in python something like foo.bar().baz()['key'] :-) > >> 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 suggest you to use the straight syntax: class C1 { var $m1; /* ... */ } class C2 extends C1 { function C2() { $this->m2 = new C1; } /* ... */ } $c2 = new C2; $c2->m2->m1='foo'; /*PHP4 only */ -- CIMPOESU Teodor, Web Programmer @ DIGICOM S.A. Bucharest, Romania @ Internet, site development @ teo@digiro.net , +(401)-330.47.28 official home page ~ http://www.digiro.net/ Internet web page ~ http://internet.digiro.net/

« previous php.version4 (#10029) next »