Bug #75158 [NEW]: Cloning objects whith members that hold references to other members

From: Date: Tue, 05 Sep 2017 15:27:33 +0000
Subject: Bug #75158 [NEW]: Cloning objects whith members that hold references to other members
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-210959@lists.php.net to get a copy of this message
From: krulis at ksi dot mff dot cuni dot cz Operating system: all PHP version: 7.1.9 Package: Class/Object related Bug Type: Bug Bug description:Cloning objects whith members that hold references to other members Description: ------------ When I have an object and one member variable is a reference to another member variable, clone of such object is not correctly constructed. In the test script, you can see Foo class with two members: $data and $ref. $ref is set to be a reference to $data of the same object. When object of foo is cloned. The clone's $data is in fact reference to original object's $data. In other words, clone should in fact do $c->data = $o->data (shallow copy by = operator), but instead, it seems like $c->data =& $o->data. Note that the references are in fact not used, and when they are removed (line in the constructor is commented), it starts to work correctly again. I have also tested integer values in $data (beside arrays) and they work all the same. This behavior was observed on Windows 7.1.9 and Linux 5.6.26. A similar problem was described in #66040, but I am not sure, whether they are connected or not. Test script: --------------- <?php class Foo { public $data = []; public $ref; public function __construct() { $this->ref =& $this->data; } } $o = new Foo(); $c = clone($o); $o->data[] = 42; var_dump($c->data); // writes out an array with one item (42), even though the array should be empty Expected result: ---------------- I would expect that $c->data === [] at the end. -- Edit bug report at https://bugs.php.net/bug.php?id=75158&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=75158&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=75158&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=75158&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=75158&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=75158&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=75158&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=75158&r=needscript Try newer version: https://bugs.php.net/fix.php?id=75158&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=75158&r=support Expected behavior: https://bugs.php.net/fix.php?id=75158&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=75158&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=75158&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=75158&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75158&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=75158&r=dst IIS Stability: https://bugs.php.net/fix.php?id=75158&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=75158&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=75158&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=75158&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=75158&r=mysqlcfg

« previous php.bugs (#210959) next »