php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30978 Append the Object into array result incorrect
Submitted: 2004-12-03 19:14 UTC Modified: 2004-12-17 09:36 UTC
From: btb_tp at yahoo dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.2 OS: Windows XP SP2
Private report: No CVE-ID: None
 [2004-12-03 19:14 UTC] btb_tp at yahoo dot com
Description:
------------
Below code work with PHP 4.3.9!!!

Reproduce code:
---------------
class A{
	protected $value = 0;
	function A($value){
		$this->value = $value;}
	function getValue(){
		return $this->value;}
	function setValue($value){
		$this->value = $value;}
}

$b = array();
$a = new A(0);
for($i = 1; $i <= 2; $i++){
	$a->setValue($i);
	$b[] = $a;}
print_r($b);

Expected result:
----------------
Array ( [0] => A Object ( [value:protected] => 1 ) [1] => A Object ( [value:protected] => 2 ) ) 

Actual result:
--------------
Array ( [0] => A Object ( [value:protected] => 2 ) [1] => A Object ( [value:protected] => 2 ) ) 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-17 03:15 UTC] levi at alliancesoftware dot com dot au
This is not a bug, this a result of the new object model in PHP 5. See http://www.zend.com/php5/articles/engine2-php-oo.php

 In PHP 4, the assignment to an object would create a new copy of that object.

 In PHP 5, assignment to an object merely sets a *reference* to that object, not a copy.
 [2004-12-17 09:36 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

What Levi said.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 10:01:30 2025 UTC