Bug #75632 [Opn->Nab]: Wrong behavior of clone and references

From: Date: Tue, 05 Dec 2017 14:55:19 +0000
Subject: Bug #75632 [Opn->Nab]: Wrong behavior of clone and references
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-212943@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75632&edit=1 ID: 75632 Updated by: requinix@php.net Reported by: kripper at imatronix dot com Summary: Wrong behavior of clone and references -Status: Open +Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: All PHP Version: 7.1.12 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php References are not pointers: $ptr does not "point" to $orig->test, rather they both use the same underlying value stored in memory. Also, creating a reference forces both sides of the assignment to be references - otherwise the system wouldn't work. http://php.net/manual/en/language.references.php If you unset($ptr) before the clone then $orig->test will become the only reference to its value left and so automatically revert to a normal unreferenced value. https://3v4l.org/c6epk Previous Comments: ------------------------------------------------------------------------ [2017-12-05 14:08:13] kripper at imatronix dot com Description: ------------ Setting a unused reference to a cloned object property, prevents the object to be cloned. This makes no sense and is intuitive. Test script: --------------- $orig = new stdClass(); $orig->test = 'original'; $ptr = & $orig->test; // <--- Setting an unused reference changes the behaviour $copy = clone $orig; $copy->test = "modified"; die("Orig is: {$orig->test}"); // Gives 'modified' instead of 'original' Expected result: ---------------- $orig->test should give 'original' since we cloned the object. A cloned object should be a cloned object, no matter of unused references. Actual result: -------------- Setting a reference makes "$copy = clone $original" to behave just like "$copy = $original" (a reference). Not setting or unsetting the refernce, fixes the behavior. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75632&edit=1

« previous php.bugs (#212943) next »