php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #47967 array_clone - would also clone referenced objects
Submitted: 2009-04-14 14:44 UTC Modified: 2014-12-30 10:41 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: r at dumb dot ro Assigned:
Status: No Feedback Package: Class/Object related
PHP Version: 5.2.9 OS: Irrelevant
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: r at dumb dot ro
New email:
PHP Version: OS:

 

 [2009-04-14 14:44 UTC] 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));

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-14 19:12 UTC] 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.
 [2011-01-23 21:14 UTC] jani@php.net
-Package: Feature/Change Request +Package: Class/Object related -Operating System: all +Operating System: *
 [2014-04-16 23:31 UTC] levim@php.net
-Status: Open +Status: Feedback -Operating System: * +Operating System: Irrelevant
 [2014-04-16 23:31 UTC] levim@php.net
Are you still interested in pursuing this?
 [2014-12-30 10:41 UTC] php-bugs at lists dot php dot net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 00:01:27 2024 UTC