Hi,

I was wondering if any of you could help me out with a problem concerning
object references. At first, I thought that it would be a bug
(http://bugs.php.net/bug.php?id=26756) but the developers told me that it is not. So 
here is a
short description:

If you have two object that reference each other, one of the references
seems to get lost. Instead I get a copy of the object. If you consider the
following source 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; // *1*

$obj2->counter++; // *2*
$obj3 =& $obj2->getTest3();
echo $obj3->getCounter(); // *4*

var_dump($obj);

--------------------

Line *1* should echo "2" after the constructor of Test2 increased the
counter. Line *2* should increase it to "3", so line *4* should echo "3". But if
you consider the result, you actually get the following:

--------------------

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)
  }
}

--------------------

Note especially the missing ampersands, although I only used references.
I hope someone can help me as I really depend on objects referencing each
other.

Tilman Giese

-- 
+++ GMX - die erste Adresse für Mail, Message, More +++
Neu: Preissenkung für MMS und FreeMMS! http://www.gmx.net

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to