From:             [EMAIL PROTECTED]
Operating system: Debian GNU/Linux (woody)
PHP version:      4.0.6
PHP Bug Type:     Scripting Engine problem
Bug description:  Object Copied By Reference!!

When objects that contain objects that are referenced
elsewhere, they are not correctly dereferenced, and strange
side effects result

class Foo{
  var $a;
  function Foo($value)
    {
      $this->set($value);
    }
  function set($value)
    {
      $this->a = $value;
    }
  function get()
    {
      return $this->a;
    }
}
class Bar{
  var $a;
  function Bar($value)
    {
      $this->a = new Foo($value);
    }
  function set($value)
    {
      $this->a->set($value);
    }
  function get()
    {
      return $this->a->get();
    }
  function share($other)
    {
      $this->a = & $other->a;
    }
}

//Objects are initialized
$obj = new Bar(1);
$obj2 = new Bar(2);

//$obj and $obj2 both contain an internal reference to the
//same Foo object, with a value of 2
$obj->share($obj2);

//$obj2 now references a new Bar object with a new
//Foo object containing a value of 3
$obj2 = new Bar(3);

//$obj2 should be a copy of $obj1 and contain a new
//Foo object with a value of 2
$obj2 = $obj;

//Somehow, this also changes the value in $obj2
$obj->set(4);

//In a sane world, this outputs 2
//In reality, we get a 4
echo $obj2->get();



-- 
Edit Bug report at: http://bugs.php.net/?id=11909&edit=1



-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to