[PHP] Classes, Constructors, References and Recursion
Hi, I have this piece of code: class test1 { var $data = 'hi', $node = false; function test1() { $this-node = new test2($this); } } class test2 { var $data = 'yoyo', $root = false; function test2($root) { $this-root = $root; } } $test = new test1(); echo('pre'); print_r($test); echo('/pre'); And it outputs: test1 Object ( [data] = hi [node] = test2 Object ( [data] = yoyo [root] = test1 Object ( [data] = hi [node] = *RECURSION* ) ) ) while it should output: test1 Object ( [data] = hi [node] = test2 Object ( [data] = yoyo [root] = *RECURSION* ) ) I know there are some difficulties using references in constructors, but I think this should be right.. I'm using PHP/4.3.11. Can anyone tell me what is wrong with this code or why PHP behaves this way? regards, Evert -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes, Constructors, References and Recursion
Evert|Rooftop Solutions wrote: Hi, I have this piece of code: class test1 { var $data = 'hi', $node = false; function test1() { $this-node = new test2($this); } } class test2 { var $data = 'yoyo', $root = false; function test2($root) { $this-root = $root; } } $test = new test1(); echo('pre'); print_r($test); echo('/pre'); And it outputs: test1 Object ( [data] = hi [node] = test2 Object ( [data] = yoyo [root] = test1 Object ( [data] = hi [node] = *RECURSION* ) ) ) while it should output: test1 Object ( [data] = hi [node] = test2 Object ( [data] = yoyo [root] = *RECURSION* ) ) does the output of print_r() correlate with whats actually happening? i.e. is the first test1 object not a reference of the second test1 object (as per the print_r() dump). what does the following show? (I don't have php4 at hand): var_dump($test); also I believe print_r() and var_dump() have a few odditities regarding display of recursion with regard to objects... internals mailinglist archive might tell you more on that. does the following show different output? ?php class test1 { var $data = 'hi', $node = false; function test1() { $this-node = new test2(); $this-node-root = $this; } } class test2 { var $data = 'yoyo', $root = false; } $test = new test1(); echo pre; print_r($test); echo hr /; var_dump($test); echo /pre; ? I know there are some difficulties using references in constructors, but I think this should be right.. I'm using PHP/4.3.11. Can anyone tell me what is wrong with this code or why PHP behaves this way? go for php5 if you can, you'll have alot more fun with object then :-) no more ''s for starters. regards, Evert -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Classes, Constructors, References and Recursion
Jochem Maas wrote: also I believe print_r() and var_dump() have a few odditities regarding display of recursion with regard to objects... internals mailinglist archive might tell you more on that. I think I have the answer, when I pass a var to print_r it isn't passed by reference, so there will be a copy made immediately. go for php5 if you can, you'll have alot more fun with object then :-) no more ''s for starters. God I wish I could! The thing is, most hosting company's don't support it yet, and since I'm making a product that should run (and is already running) at various places, I have to use PHP4 :( thanx man, Evert -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php