From:             php dot bugs at darwin dot no-ip dot com
Operating system: Linux 2.4.18
PHP version:      4.3.4
PHP Bug Type:     Scripting Engine problem
Bug description:  Object copy neither shallow nor deep

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 bug report at http://bugs.php.net/?id=27399&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=27399&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=27399&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=27399&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=27399&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=27399&r=needtrace
Need Reproduce Script:      http://bugs.php.net/fix.php?id=27399&r=needscript
Try newer version:          http://bugs.php.net/fix.php?id=27399&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=27399&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=27399&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=27399&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=27399&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=27399&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=27399&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=27399&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=27399&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=27399&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=27399&r=float

Reply via email to