php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #17938 Invalid object copy
Submitted: 2002-06-24 04:04 UTC Modified: 2002-09-11 11:24 UTC
From: jacob at inow8 dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.2.1 OS: Window 2000
Private report: No CVE-ID: None
 [2002-06-24 04:04 UTC] jacob at inow8 dot com
The below example shows how $a is copyed (=) into $b but dosent make a "copy" or dos it?

Its about the prob properti, i asign an object "Obj" to $a->prob, and sets a new properti, $a->prob->p1, then i run the method $a->prob->PrintObj(), and copyes $a it into $b, and asign $b->prob->p2 to the cloned version of $b, and $a->prob->p3 to the original version of $a.

If this sould be right then $a->prob will have p1 and p3, and $b->prob will have p1 and p2... but because of the PrintObj method thats not the case... and it has nothing to do with the name and action of the method!

	class Obj {
		function Obj() {
			$this->name = 'a';
		}
		function PrintObj() {
			echo 'Printing...';
		}
	}
	
	$a = new Obj();
	$a->prob = new Obj();
	$a->prob->p1 = 'test';
	
	//This is the problem... running a method (any method) before copying!
	//Causing a "reference" only to the "prob" properti in the copy version.
	$a->prob->PrintObj();
	
	$b = $a;

	$b->prob->p2 = 'test';
	$a->prob->p3 = 'test';
	
	print_r($a);
	print_r($b);
	
	/*
	Printing...obj Object
	(
		[name] => a
		[prob] => obj Object
			(
				[name] => a
				[p1] => test
				[p2] => test
				[p3] => test
			)
	
	)
	obj Object
	(
		[name] => b
		[prob] => obj Object
			(
				[name] => a
				[p1] => test
				[p2] => test
				[p3] => test
			)
	
	)
	*/
	
	$a = new Obj();
	$a->prob = new Obj();
	$a->prob->p1 = 'test';
	
	//This code dos what i whant, because i dont execute the PrintObj method????

	$b = $a;

	$b->prob->p2 = 'test';
	$a->prob->p3 = 'test';
	
	print_r($a);
	print_r($b);
	
	/*
	obj Object
	(
		[name] => a
		[prob] => obj Object
			(
				[name] => a
				[p1] => test
				[p3] => test
			)
	
	)
	obj Object
	(
		[name] => b
		[prob] => obj Object
			(
				[name] => a
				[p1] => test
				[p2] => test
			)
	
	}
	*/

Your sincerely
Jacob Lindborg

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-24 04:06 UTC] jacob at inow8 dot com
The below example shows how $a is copyed (=) into $b but dosent make a
"copy" or dos it?

Its about the prob properti, i asign an object "Obj" to $a->prob, and
sets a new properti, $a->prob->p1, then i run the method
$a->prob->PrintObj(), and copyes $a it into $b, and asign $b->prob->p2
to the cloned version of $b, and $a->prob->p3 to the original version of
$a.

If this sould be right then $a->prob will have p1 and p3, and $b->prob
will have p1 and p2... but because of the PrintObj method thats not the
case... and it has nothing to do with the name and action of the
method!

	class Obj {
		function Obj() {
			$this->name = 'a';
		}
		function PrintObj() {
			echo 'Printing...';
		}
	}
	
	$a = new Obj();
	$a->prob = new Obj();
	$a->prob->p1 = 'test';
	
	//This is the problem... running a method (any method) before copying!
	//Causing a "reference" only to the "prob" properti in the copy
version.
	$a->prob->PrintObj();
	
	$b = $a;

	$b->prob->p2 = 'test';
	$a->prob->p3 = 'test';
	
	print_r($a);
	print_r($b);
	
	/*
	Printing...obj Object
	(
		[name] => a
		[prob] => obj Object
			(
				[name] => a
				[p1] => test
				[p2] => test
				[p3] => test
			)
	
	)
	obj Object
	(
		[name] => b
		[prob] => obj Object
			(
				[name] => a
				[p1] => test
				[p2] => test
				[p3] => test
			)
	
	)
	*/
//---------------------------------------------------
//Now lets try without the method!!!
//---------------------------------------------------	
	$a = new Obj();
	$a->prob = new Obj();
	$a->prob->p1 = 'test';
	
	//This code dos what i whant, because i dont execute the PrintObj
method????

	$b = $a;

	$b->prob->p2 = 'test';
	$a->prob->p3 = 'test';
	
	print_r($a);
	print_r($b);
	
	/*
	obj Object
	(
		[name] => a
		[prob] => obj Object
			(
				[name] => a
				[p1] => test
				[p3] => test
			)
	
	)
	obj Object
	(
		[name] => b
		[prob] => obj Object
			(
				[name] => a
				[p1] => test
				[p2] => test
			)
	
	}
	*/

Your sincerely
Jacob Lindborg
 [2002-06-24 04:18 UTC] mfischer@php.net
Please test http://www.php.net/distributions/php-4.3.0-dev-zend2-alpha1pl1.tar.gz , I'm pretty sure this sort of behaviour is fixed in ZE2.
 [2002-09-11 11:24 UTC] sniper@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.

ZE2 thing..

 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 15:00:01 2026 UTC