| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
  [2000-10-28 10:25 UTC] waldschrott@php.net
 This code is pretty self-explanatory...
Uncommenting the noticed (*[0]) line, causes the object contained ('root') in the instance of 'obj'  to be referenced to whatever, function test() uses a copy $o_copy but reference-counting fails and the contained object 'root' is somehow referenced to the original object in the global scope.
After having uncommented *[0] try to uncomment *[1] what should break the reference again, that made me assuming this all is caused by reference counting...
<?php
class obj {
	function method() {}
    }
function test($o_copy) {
	//$o_copy->root=&$o_copy->root; // *[1]
	$o_copy->root->set_in_copied_o=TRUE;
 	var_dump($o_copy);?><BR><?php }
$o->root=new obj();
//$o->root->method();	// *[0] uncomment this line and weirdness starts
var_dump($o);?><BR><?php
test($o);
var_dump($o);
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             | 
    |||||||||||||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 00:00:01 2025 UTC | 
I have reproducted this problem... and am pretty sure what is exactly is going wrong... <? class foo { function foo() { $this->bar = new bar(); } } class bar { function bar() { $this->tmp = "bar"; } function do_nothing() { } } $foo = new foo(); $foo->bar->tmp = "test"; $foo2 = $foo; $foo2->bar->tmp = "foo"; var_dump($foo); var_dump($foo2); // now.... $foo->bar->do_nothing(); $foo3 = $foo; $foo3->bar->tmp = "bug"; var_dump($foo); var_dump($foo3); ?> Basically when you call a method from a objects member object. It creates that member as a refrence. /* i set this to critical because i think this should be resloved right away */ - BradPS. I narrowed down the test: class obj { function method() { } } $o->root=new obj(); var_dump($o); $o->root->method(); var_dump($o); and its output with PHP 4.2.3 compiled for m68k-amigaos: object(stdClass)(1) { ["root"]=> object(obj)(0) { } } object(stdClass)(1) { ["root"]=> &object(obj)(0) { } } (note the ampersand in the second dump), while, with PHP 5.2.14 for Windows, the output is: object(stdClass)#2 (1) { ["root"]=> object(obj)#1 (0) { } } object(stdClass)#2 (1) { ["root"]=> object(obj)#1 (0) { } } Does it make sense that in the second assign "$o->root->method();", the method is viewed as a reference to and object? Please forgive me if my question is silly, I am a newbie in OO PHP.