php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26673 Nested object reference count not incremented when object copied?
Submitted: 2003-12-19 18:17 UTC Modified: 2004-01-06 20:52 UTC
From: samlw at aol dot com Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 4.3.4 OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2003-12-19 18:17 UTC] samlw at aol dot com
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)   } } 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-01 20:56 UTC] sniper@php.net
Please provide a bit better script than this.
And what makes you think this isn't expected behaviour?


 [2004-01-06 20:52 UTC] sniper@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Fri Dec 09 02:05:54 2022 UTC