Req #47967 [Fbk->NoF]: array_clone - would also clone referenced objects
| From: | php-bugs at lists dot php dot net | Date: | Tue, 30 Dec 2014 10:41:32 +0000 |
| Subject: | Req #47967 [Fbk->NoF]: array_clone - would also clone referenced objects | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189385@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: php-bugs@lists.php.net
Reported by: r at dumb dot ro
Summary: array_clone - would also clone referenced objects
-Status: Feedback
+Status: No Feedback
Type: Feature/Change Request
Package: Class/Object related
Operating System: Irrelevant
PHP Version: 5.2.9
Private report: N
New Comment:
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
Previous Comments:
------------------------------------------------------------------------
[2014-04-16 23:31:36] levim@php.net
Are you still interested in pursuing this?
------------------------------------------------------------------------
[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