php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26756 object references get lost
Submitted: 2003-12-31 10:10 UTC Modified: 2004-01-01 20:52 UTC
From: tilman dot giese at gmx dot de Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.3.4 OS: Linux
Private report: No CVE-ID: None
 [2003-12-31 10:10 UTC] tilman dot giese at gmx dot de
Description:
------------
The problem is not that easy to explain. If you have two objects referencing each other, one of the references gets somehow lost. Look at the actual result, especially at the missing ampersands of object(test3)!

Although I only use references in the script (sorry about it be longer than 20 lines) it seems that I get copies of the objects.

Reproduce code:
---------------
class Test1 {

    var $a;

    function Test1() {

    	$this->a = new Test2();
    }

    function &getTest2() {

    	return $this->a;
    }
}

class Test2 {

	var $b;

	var $counter = 1;

	function Test2() {

    	$this->b = new Test3($this);
    }

    function &getTest3() {

    	return $this->b;
    }
}

class Test3 {

	var $p;

	function Test3(&$parent) {

    	$parent->counter++;

        $this->p =& $parent;
    }

    function getCounter() {

    	return $this->p->counter;
    }
}

$obj = new Test1();
$obj2 =& $obj->getTest2();
echo $obj2->counter;

$obj2->counter++;
$obj3 =& $obj2->getTest3();
echo $obj3->getCounter();

var_dump($obj);

Expected result:
----------------
23

object(test1)(1) {
  ["a"]=>
  &object(test2)(2) {
    ["b"]=>
    &object(test3)(1) {
      ["p"]=>
      &object(test2)(2) {
        ["b"]=>
        &object(test3)(1) {
          ["p"]=>
          &object(test2)(2) {
            ["b"]=>
            &object(test3)(1) {
              ["p"]=>
              *RECURSION*
            }
            ["counter"]=>
            int(3)
          }
        }
        ["counter"]=>
        int(3)
      }
    }
    ["counter"]=>
    int(3)
  }
}


Actual result:
--------------
22

object(test1)(1) {
  ["a"]=>
  &object(test2)(2) {
    ["b"]=>
    &object(test3)(1) {
      ["p"]=>
      &object(test2)(2) {
        ["b"]=>
        object(test3)(1) {
          ["p"]=>
          &object(test2)(2) {
            ["b"]=>
            object(test3)(1) {
              ["p"]=>
              *RECURSION*
            }
            ["counter"]=>
            int(2)
          }
        }
        ["counter"]=>
        int(2)
      }
    }
    ["counter"]=>
    int(3)
  }
}


Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-01 20:52 UTC] sniper@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Aug 19 10:01:29 2024 UTC