ID: 38788
Updated by: [EMAIL PROTECTED]
Reported By: j dot henge-ernst at interexa dot de
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: linux
PHP Version: 5.1.6
-Assigned To:
+Assigned To: dmitry
New Comment:
This is not a BUG, but expected behavior.
look into your __clone() method. $this->a['f'] and $this->b are
references to the same variable (to $x1->b). You destroy $this->a['f'],
but not $this->b that still point ot $x1->b, and makes $this->a['f']
reference to the same var again.
You would probably like change your code to:
public function __clone() { unset($this->b); $this->a['f'] =
&$this->b; }
Now it works as you expected.
Previous Comments:
------------------------------------------------------------------------
[2006-09-12 12:18:05] j dot henge-ernst at interexa dot de
Description:
------------
I encountered a problem similar to the bug #27268
http://bugs.php.net/bug.php?id=27268
The attribut b of the cloned object is still pointing to the attribut
of its original object. So changing the attribut in the cloned object
also changes the attribute in the original object.
Reproduce code:
---------------
<?php
class a {
protected $a = array();
protected $b = '';
public function __construct($b) { $this->b = $b; $this->a['f'] =
&$this->b; }
public function setB($b) { $this->b = $b; }
public function printB() { echo __CLASS__ . $this->b . '/' .
$this->a['f'] ."\n"; }
public function __clone() { unset($this->a['f']); $this->a['f'] =
&$this->b; }
}
$x1 = new a(5);
$x2 = clone $x1;
$x2->setB(7);
$x1->printB();
$x2->printB();
Expected result:
----------------
a5/5
a7/7
Actual result:
--------------
a7/7
a7/7
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=38788&edit=1