URGENT: References
| From: | Martin Thoma | Date: | Sat, 02 Sep 2000 18:29:28 +0000 |
| Subject: | URGENT: References | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-14937@lists.php.net to get a copy of this message | ||
Hello !
I got a problem with references (I think). I want to make a
tree-like-structure with a class MenuItem. The childs of one item are
stored into the array MenuItem::childs.
The problem:
When returning a child-MenuItem, it's not the same object I inputted
before. I think, this is because PHO is making a copy of the object when
returning it.
In the following sample, "something went wrong" is printed, but I want
to be $foundItem the same as $firstChild . I tried to return the child
by reference (something like "return &$this->childs[0];"), but I only
got parse-errors.
Any help ?
Regards
Martin
$root = new MenuItem(0);
$firstChild = new MenuItem($root);
$foundItem = $root->FindChild();
if ($foundItem == $firstChild)
print "It works !!";
else print "The objects are the same, but something went wrong";
class MenuItem
{ var $parent; // Parent of the item
var $childs = array(); // All Child-Items
// Konstruktor
function MenuItem($parent)
{ $this->parent = $parent;
if ($parent != 0)
{ $parent->AddChild($this);
}
}
// Fügt ein Kind hinzu
function AddChild($childItem)
{ array_push($this->childs, &$childItem);
}
function FindChild()
{ return $this->childs[0]; // for Testing only
}
}