ID: 27399 Updated by: [EMAIL PROTECTED] Reported By: php dot bugs at darwin dot no-ip dot com -Status: Open +Status: Bogus Bug Type: Scripting Engine problem Operating System: Linux 2.4.18 PHP Version: 4.3.4 New Comment:
To get the expected result, change every "= new" to "=& new". Previous Comments: ------------------------------------------------------------------------ [2004-02-25 16:39:02] php dot bugs at darwin dot no-ip dot com Description: ------------ Copying an object doesn't appear to work as expected for either a shallow or a deep copy. Reproduce code: --------------- class Dummy {} $listA = new Dummy(); $listA->next = new Dummy(); $listA->next->previous =& $listA; $listA->value = "one"; $listA->next->value = "two"; $listA->next->previous->value = "ONE"; echo "ListA: "; echo "(" . $listA->value . ", " . $listA->next->value . ") \n"; $listB = $listA; $listB->value = "three"; $listB->next->value = "four"; $listB->next->previous->value="THREE"; echo "(modified listB)\n"; echo "ListA: "; echo "(" . $listA->value . ", " . $listA->next->value . ") \n"; echo "ListB: "; echo "(" . $listB->value . ", " . $listB->next->value . ") \n"; Expected result: ---------------- Expected output: (ie: if PHP does a deep copy) ListA: (ONE, two) (modified listB) ListA: (ONE, two) ListB: (three, four) This should happen because $listB->next->previous is a copy of the reference to $listA. Modifying it should not modify ListA OR ListB. Possible (wrong) output: (if PHP had done a shallow copy) ListA: (ONE, two) (modified listB) ListA: (THREE, four) ListB: (three, four) This would be the case because listB's next would be a reference to ListA's next. Actual result: -------------- Actual output: ListA: (ONE, two) (modified listB) ListA: (THREE, two) ListB: (three, four) This looks like PHP did something between a shallow and a deep copy. while ListB->next is a copy, ListB->next->previous still points at ListA. (!?!) ... For an added bonus, try replacing '= new' with '=& new' and it looks like PHP does a shallow copy. From my reading of PHP documentation, I thought PHP would always do deep (de-referencing) copies. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=27399&edit=1