|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-01-01 20:56 UTC] sniper@php.net
[2004-01-06 20:52 UTC] sniper@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 25 05:00:01 2025 UTC |
Description: ------------ In certain cases, the reference counting mechanism seems to become confused when dealing with nested objects. If class 'outer' has a member that is an object of class 'inner', then a copy of an instance of outer sometimes shares the original's reference to inner. As a result, if a change is made to the inner member of the copy, the original's inner is also changed. See the attached code for a working example of this problem. I have reproduced this problem in v 4.3.2 and 4.3.4 running on OS X. Reproduce code: --------------- class inner { var $val1; var $val2; function inner ($val1, $val2) { $this->set($val1, $val2); } function set ($val1, $val2) { $this->val1 = $val1; $this->val2 = $val2; } } class outer { var $val0; var $inner; function outer ($val0, $val1, $val2) { $this->val0 = $val0; $this->inner = new inner($val1, $val2); } function set ($val0, $val1, $val2) { $this->val0 = $val0; $this->inner->set($val1, $val2); } } // construct and dump an object with values [0, 1, 2] $myOuter = new outer(0, 1, 2); var_dump($myOuter); echo '<br />--------<br />'; // change values to [3, 4, 5] $myOuter->set(3, 4, 5); var_dump($myOuter); echo '<br />--------<br />'; // make a COPY and change values to [6, 7, 8] $myOuterCopy = $myOuter; $myOuterCopy->set(6, 7, 8); var_dump($myOuterCopy); echo '<br />--------<br />'; // dump original - should still be [3, 4, 5], but it is [3, 7, 8]!!! // the inner object is not getting copied on modification!!! var_dump($myOuter); echo '<br />--------<br />'; Expected result: ---------------- The final var_dump should produce: object(outer)(2) { ["val0"]=> int(3) ["inner"]=> &object(inner)(2) { ["val1"]=> int(4) ["val2"]=> int(5) } } Actual result: -------------- The final var_dump produces: object(outer)(2) { ["val0"]=> int(3) ["inner"]=> &object(inner)(2) { ["val1"]=> int(7) ["val2"]=> int(8) } }