Bug #11543 Updated: Calling an object's method changes the variable to a reference to this object.

From: Date: Wed, 30 Jan 2002 17:28:49 +0000
Subject: Bug #11543 Updated: Calling an object's method changes the variable to a reference to this object.
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-78571@lists.php.net to get a copy of this message
ID: 11543 Comment by: colleen@surfmerchants.com Old Reported By: gagi@fin.de Reported By: colleen@surfmerchants.com Status: Open Bug Type: Class/Object related Operating System: Linux PHP Version: 4.0.4pl1 New Comment: I'm definitely experiencing this bug, and it causes me to lose data because the objects I'm using are contained in a session variable. Because sessions don't transmit references, I lose any object whose function I have called. Right now I'm using a work-around where I assign the object to another, temporary object, run the temp's function, and then copy the temp back to the real object. That seems to work. Is this problem fixed in newer versions of PHP? I'd rather not have to use the work-around if it's not necessary. Thanks. Previous Comments: ------------------------------------------------------------------------ [2001-06-18 16:02:48] gagi@fin.de Hello! The following code defines two classes. The first class (COuter) contains a variable to which an instance of the second class (CInner) is assigned. After initializing this hierarchy, the script dumps out the hierarchy's internal structure and tests if it its possible to copy the hierarchy. Now the script calls method of the second object. This method just outputs a short text and returns. After returning the script creates a new dump and tests the possibility to copy the hierarchy again. The two var-dumps should look like these: Dump 1: object(couter)(2) { ["T"]=> string(0) "" ["Inner"]=> object(cinner)(1) { ["T"]=> string(0) "" } } Here the method is called! Dump 2: object(couter)(2) { ["T"]=> string(0) "" ["Inner"]=> &object(cinner)(1) { ["T"]=> string(0) "" } } If you look closely at the two dumps, you will see, that variable Inner becomes a reference in the second dump! A look on the copy-tests verify the suppositon: The second copy-test does not copy the object-hierarchy completely! All I have to do to change the variable to a reference is: call a function of the inner class! I hope my description and the following code will help you to find the bug, if it is one as I suppose. Greetings, Christoph Boehme. <? // Defining the classes: class COuter {var $T, $Inner;} class CInner{ var $T; function Func() {echo("Calling CInner::Func();<br>");} } // Creating the object hierarchy: $MyOuter =new COuter; $MyOuter->T =""; $MyOuter->Inner =new CInner; $MyOuter->Inner->T =""; // Dumping out the hierarchy's internal structure: echo("Object structure of \$MyOuter:<br>"); var_dump($MyOuter); echo("<br>"); // Testing if it is possible to copy the hierarchy: $Copy1 =$MyOuter; $Copy1->T ="Test"; $Copy1->Inner->T ="Test"; echo("\$Copy1->T: ". $Copy1->T ."<br>"); echo("\$MyOuter->T: ". $MyOuter->T ."<br>"); echo("\$Copy1->Inner->T: ". $Copy1->Inner->T ."<br>"); echo("\$MyOuter->Inner->T: ". $MyOuter->Inner->T ."<br><br>"); // Calling a method of the inner object: $MyOuter->Inner->Func(); // Dumping out the hierarchy's structure again: echo("<br>Object structure of \$MyOuter:<br>"); var_dump($MyOuter); echo("<br>"); // Testing again if it is possible to copy the hierarchy: $Copy2 =$MyOuter; $Copy2->T ="Test"; $Copy2->Inner->T ="Test"; echo("\$Copy2->T: ". $Copy2->T ."<br>"); echo("\$MyOuter->T: ". $MyOuter->T ."<br>"); echo("\$Copy2->Inner->T: ". $Copy2->Inner->T ."<br>"); echo("\$MyOuter->Inner->T: ". $MyOuter->Inner->T ."<br>"); ?> ------------------------------------------------------------------------ Edit this bug report at http://bugs.php.net/?id=11543&edit=1

« previous php.dev (#78571) next »