Re: [PHP4BETA] Returning objects
| From: | Zeev Suraski | Date: | Wed, 24 Nov 1999 20:59:38 +0000 |
| Subject: | Re: [PHP4BETA] Returning objects | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-6866@lists.php.net to get a copy of this message | ||
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 {It currently isn't. It's something that's planned for a future version of PHP, but not 4.0 (most probably 4.1). Zeev -- Zeev Suraski <zeev@zend.com> http://www.zend.com/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?