At 03:36 08.11.2002, Tim Molendijk said:
--------------------[snip]--------------------
>begin first code snippet --------------------------------
><?php
>/* New Container object created. At the same time it sets a Child object in
>its $child attribute. (This is indicated by passing TRUE.) */
> $testContainer1 = new Container(TRUE);
Ahh - culprit 1:
You said the container is creating a child instance - I assume it's done in
the constructor...
What you're doing here is to create a clone when assigning new()'s result
to $testContainer1. You should rather
$testContainer1 =& new Container(TRUE);
Note the ampersand here - if you don't you'll get a clone.
Rule for references:
If your object does something in its constructor, use a reference
assignment for
new class()
>Below the code for the classes Container en Child.
>
>begin third code snippet --------------------------------
><?php
>
>class Container
>{
> var $children;
> var $property;
>
> // stuff deleted
>
> function load()
> {
> $newChild = new Child(1);
Again - make this
$newChild =& new Child(1);
> function add($child)
> {
>/* Here a reference of $this (the Container object) is assigned to $child's
>$parent attribute. */
> $child->parent = &$this;
>/* Here $child (the newly created Child object) is assigned to the
>container's $child property. */
> $this->child = &$child;
> }
But you don't pass "$child" by reference! Make this
function (&$child)
If you don't pass $child as a reference, the line
$child->parent = $this;
will set the parent value of the clone that's passed as an argument, and
leave the "original" $child unmodified!
Crosscheck all of your class code (and the code utilizing it) it you're
always passing references. Ref's work like a charm until you mess up at a
single location. Well, having made my way from C/C++ to PHP I'd rather deal
with pointers, but unfortunately PHP doesn't have them. So keep an eye open .-)
--
>O Ernest E. Vogelsinger
(\) ICQ #13394035
^ http://www.vogelsinger.at/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php