Bug #66040 [NEW]: Cloning an object while having reference to members results incorrect clone
| From: | bawolff+wn at gmail dot com | Date: | Thu, 07 Nov 2013 02:54:14 +0000 |
| Subject: | Bug #66040 [NEW]: Cloning an object while having reference to members results incorrect clone | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-182621@lists.php.net to get a copy of this message | ||
From: bawolff+wn at gmail dot com
Operating system: linux
PHP version: 5.5Git-2013-11-07 (snap)
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Cloning an object while having reference to members results incorrect clone
Description:
------------
The problem is easiest understood by looking at the test script.
Basically, if you clone an object in a function that as one of its
arguments (or one of its calling functions) has a reference to a member
variable of this object, the clone of the object is not correct for the
field that the reference is to. The fields of the two objects behave as
if they are references to each other (To clarify I do not mean fields
point to the same object as is expected, I mean the field itself is a
reference to the other field.). If you assign something to that member
variable, the change is reflected in both the cloned object and the
original, where it should only be reflected in the object that the
assignment was for.
Tested on snapshot php5.5-201311070030
Also tested on earlier versions like 5.3.3-7
Originally discovered while investigating a bug in MediaWiki:
https://bugzilla.wikimedia.org/show_bug.cgi?id=56226
Test script:
---------------
<?php
$originalObj = new A;
class A {
var $foo = 'default value';
}
// Important note: If you remove the &, the bug disappears.
function changeFooWithUnusedReference( &$unusedReferenceToFoo, $newValue
) {
global $originalObj;
$newObj = clone $originalObj;
echo $originalObj->foo . "\n";
echo $newObj->foo . "\n";
$newObj->foo = $newValue;
echo $originalObj->foo . "\n";
echo $newObj->foo . "\n";
}
changeFooWithUnusedReference( $originalObj->foo, 'Some other value' );
Expected result:
----------------
I expect the following output:
default value
default value
default value
Some other value
Actual result:
--------------
Following output:
default value
default value
Some other value
Some other value
--
Edit bug report at https://bugs.php.net/bug.php?id=66040&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=66040&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=66040&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=66040&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=66040&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=66040&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=66040&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=66040&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=66040&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=66040&r=support
Expected behavior: https://bugs.php.net/fix.php?id=66040&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=66040&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=66040&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=66040&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66040&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=66040&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=66040&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=66040&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66040&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=66040&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=66040&r=mysqlcfg