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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 18:01:28 2024 UTC