"Ernest E Vogelsinger" <[EMAIL PROTECTED]> wrote in message news:5.1.1.6.2.20021108092657.02befe20@;mail.vogelsinger.at... > At 03:36 08.11.2002, Tim Molendijk said: > --------------------[snip]-------------------- > > 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() >
Ernest, Before your post I accidently found out a solution: $testContainer1 = &new Container(TRUE) instead of $testContainer1 = new Container(TRUE); When I found this out I didn't understand WHY this was the solution... Thanks to your comments I DO understand why... I suddenly see every jigsaw piece fall in place :D The difference between the first code snippet (that with $testContainer1) and the second code snippet (that with $testContainer2) is that in code snippet 2 the Container object is already constructed and assigned to $testContainer2 when loading it. Then when it is loaded $this (in add()) refers to the object in $testContainer2 so no problem occurs. But in code snippet 1 the Container object loads itself before it is assigned to $testContainer1. During this process $this (in add()) refers to the object that is being created. And now the core is: the object that is being created is *not* the same object as the one that is assigned to $testContainer1!! So the references to the created object will not refer to $testContainer1!! That's it!!! I completely understand... Thanks a lot Ernest! > > Again - make this > $newChild =& new Child(1); > > > 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! > This is not true in my case, because instead of doing: function add(&$child) { ... } I do: function add($child) { ... } and then call it with: $this->add(&$child); This has the same result... As far as I know I doesn't matter whether you put an ampersand before the variable in your function definition or your in your function call. > 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 .-) > The problem is solved and I completely understand why now. This last thing is thanks to you Ernest. Regards, Tim > > -- > >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