php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50681 array of objects: copying passes reference instead of copy
Submitted: 2010-01-06 19:15 UTC Modified: 2010-01-07 09:04 UTC
From: israel at toxinworks dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.12 OS: RedHat EL5
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: israel at toxinworks dot com
New email:
PHP Version: OS:

 

 [2010-01-06 19:15 UTC] israel at toxinworks dot com
Description:
------------
PHP 5 does not copy object from array[ndx] to a new array entry, instead it seems to pass a reference to the original.

Reproduce code:
---------------
function fetchData()
{
   // return array of objects
}

$arr = fetchData();
$newNdx = count($arr);
$arr[$newNdx] = $arr[0]; // create copy of first object
$arr[$newNdx]->description = 'Change description';



Expected result:
----------------
The result is that $arr[0]->description will also have changed when we modified $arr[$newNdx]->description, therefor not copying, but more like creating a reference to the original.

This happens only in PHP 5, not 4.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-06 20:23 UTC] jani@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


 [2010-01-07 09:04 UTC] israel at toxinworks dot com
Hello, if not a bug it certainly is a very undesirable effect, I don't see why the assignment operator should pass a reference instead of a copy.

Best regards,

Israel
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 23:01:25 2025 UTC