php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11543 Calling an object's method changes the variable to a reference to this object.
Submitted: 2001-06-18 16:02 UTC Modified: 2002-06-08 06:32 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: gagi at fin dot de Assigned:
Status: Closed Package: Class/Object related
PHP Version: ZE1 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gagi at fin dot de
New email:
PHP Version: OS:

 

 [2001-06-18 16:02 UTC] gagi at fin dot 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>");
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-30 12:28 UTC] colleen at surfmerchants dot com
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.
 [2002-02-26 21:36 UTC] yohgaki@php.net
The version of PHP that this bug was reported in is too old. Please
try to reproduce this bug in the latest version of PHP (available
from http://www.php.net/downloads.php

If you are still able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".


 [2002-06-08 00:33 UTC] zionglau at yahoo dot com
Still found in lastest version 4.2.1
 [2002-06-08 06:32 UTC] mfischer@php.net
That's the way Zend Engine 1 works.

In Zend Engine 2 (ZE2) this will be solved, i.e. you have a __clone() method which performs a 1:1 clone (default implementation, this does not clone object which are properties) or you can implement your own __clone function which does the extra stuff you need (clone object which are properties).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 15:01:28 2024 UTC