Req #47967 [Opn->Fbk]: array_clone - would also clone referenced objects
| From: | levim@php.net | Date: | Wed, 16 Apr 2014 23:31:37 +0000 |
| Subject: | Req #47967 [Opn->Fbk]: array_clone - would also clone referenced objects | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-185312@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=47967&edit=1
ID: 47967
Updated by: levim@php.net
Reported by: r at dumb dot ro
Summary: array_clone - would also clone referenced objects
-Status: Open
+Status: Feedback
Type: Feature/Change Request
Package: Class/Object related
-Operating System: *
+Operating System: Irrelevant
PHP Version: 5.2.9
Block user comment: N
Private report: N
New Comment:
Are you still interested in pursuing this?
Previous Comments:
------------------------------------------------------------------------
[2009-04-14 19:12:47] code at dumb dot ro
One more thing I'd like to add:
In order for this function to work properly and for cloning in general, I think PHP would need 2
different kinds of references, an implicit and an explicit one.
Basically if I'd say $arr = array(new Obj()); it would use an implicit reference, therefore
when I'll clone the array the object would also be cloned. If I say $arr = array(&new
Obj()); that would be an explicit reference so I want the array to be an array of references and
therefore the referenced object won't be cloned.
This subject should be discussed in detail before implementation, because it has huge ramifications,
basically the whole cloning process could be changed.
------------------------------------------------------------------------
[2009-04-14 14:44:49] r at dumb dot ro
Description:
------------
When you pass an array of objects to a function (not by reference) and you modfiy one of the objects
in the array, the main array (the referenced objects) will be modified. This wasn't a problem
until objects got passed by reference by default.
Reproduce code:
---------------
$class A
{
}
$a = new A();
$a->x = 10;
$arr = array($a);
function foo($arr)
{
$arr[0]->x = 1;
}
foo($arr);
print_r($arr);
Expected result:
----------------
the expected result would be 10
Actual result:
--------------
since the array is internally using references for the stored objects the result will be 1.
to fix this I wrote a function that clones the array properly and I'm calling the function like
this:
foo(array_clone($arr));
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=47967&edit=1