#50681 [Bgs]: array of objects: copying passes reference instead of copy
ID: 50681
User updated by: israel at toxinworks dot com
Reported By: israel at toxinworks dot com
Status: Bogus
Bug Type: Class/Object related
Operating System: RedHat EL5
PHP Version: 5.2.12
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2010-01-06 20:23:45] 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-06 19:15:15] 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.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50681&edit=1
Thread (3 messages)