Returning objects
| From: | Joel Palmius | Date: | Wed, 24 Nov 1999 19:13:14 +0000 |
| Subject: | Returning objects | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-6863@lists.php.net to get a copy of this message | ||
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?
// Joel