ID: 47967 Comment by: code at dumb dot ro Reported By: r at dumb dot ro Status: Open Bug Type: *General Issues Operating System: all PHP Version: 5.2.9 New Comment:
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. Previous Comments: ------------------------------------------------------------------------ [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 http://bugs.php.net/?id=47967&edit=1