Re: [PHP4BETA] Returning objects

From: Date: Fri, 26 Nov 1999 11:52:55 +0000
Subject: Re: [PHP4BETA] Returning objects
References: 1  Groups: php.version4 
Request: Send a blank email to php-version4+get-6914@lists.php.net to get a copy of this message
On Fri, 26 Nov 1999, Joel Palmius wrote: > > On Wed, 24 Nov 1999, Zeev Suraski wrote: > > > At 21:13 24/11/1999 , Joel Palmius wrote: > > >Sorry if I missed the correct part of documentation. If this is the case, > > >could someone point me to the correct URL? Otherwise: > > > > > >Let's say I have a linked list/hierarchy: > > > > > > class TreeNode > > > { > > > var parent; > > > var children; > > > var nodename; > > > ... > > > > > > Function GetFirstChild() > > > { > > > return $this->children[0]; > > > } > > > ... > > > } > > > > > >(children is an array of TreeNode). Now let's say I have an object > > >("$mynode") of the TreeNode class and want to change the nodename of the > > >first child: > > > > > > $child = $mynode->GetFirstChild(); > > > $child->nodename = "Hello"; > > > > > >This works ok, only what I change is a *copy* of the real child, and not > > >the child itself. When doing this afterwards: > > > > > > $child = $mynode->GetFirstChild(); > > > echo $child->nodename; > > > > > >.. I get the old nodename. Now, how do I get around this? Since this is > > >the syntax of copying a variable itself and not the value of the variable: > > > > > > $a = &$b; > > > > > >.. I would believe the syntax of returning a variable reference (in > > >"GetFirstChild()" in the above example) would be: > > > > > > return &$this->children[0]; > > > > > >or, if this isn't possible, to try to get a reference the other way: > > > > > > $child = &$mynode->GetFirstChild(); > > > > > >Neither seems to work though (I get "parse error"). So, to sum up, my > > >question is how to return an object from a method in such a way that I > > >can change the parameters of the object (and not of a copy of the object) > > >outside of the class. Is it even possible? > > > > It currently isn't. It's something that's planned for a future version of > > PHP, but not 4.0 (most probably 4.1). > > Doesn't the current way in practise exclude even the possibility of > several of the most common operations in OO? From what I've gathered, most > other languages would return an object reference in the example above. > Wouldn't this a better way to handle things? In the risk of sounding like a broken record, PHP isn't an OO language (there are plenty of other OO things you can't do in PHP, its OO scope is very limited). Returning a reference in this specific case is inconsistent with the way the rest of PHP works, which is why it's not an option IMO. An optional way of returning a reference is a good thing, but in the current Zend architecture, it's not possible. It'll have to wait for version 1.1 of Zend. Zeev -- ----------------------------------------------------- Zeev Suraski <zeev@zend.com> http://www.zend.com/

« previous php.version4 (#6914) next »