From:             phallstrom at gmail dot com
Operating system: FreeBSD 4.9-RELEASE
PHP version:      4.3.11
PHP Bug Type:     Arrays related
Bug description:  Strange copy on change error with arrays, objects,  and 
serialize

Description:
------------
I came across a very odd bug in 4.3.10 (and 4.3.11).  It's not in 4.3.4,
and it's not in 5.0.4.

My understanding is that in 4.x objects are assigned by value.  So, why in
the first part of the code is the output of serialize() indicating a
reference?  This would suggest that objects are being copied by
reference.

But in the second when I change $ary[1]->name, the output of the serialize
only changes the second element, not both.  Also, changing $obj->name
doesn't have any effect on $ary[0] or $ary[1]. Which would suggest that
$ary[0] and $ary[1] are NOT the SAME thing.

The output below is from 4.3.11.  4.3.4 does not have this problem and the
serialize() output does not indicate any reference.

In 5.0.4, the everything works like I'd expect it to since objects are
assigned by reference.

It's almost like $obj is partially being copied by reference and that
serialize is picking up on that or something...

Reproduce code:
---------------
<?php

$obj->name = "Homer";
$ary[] = $obj;
$ary[] = $obj;

$obj1->name = "Homer";
$ary[] = $obj1;

print_r($ary);
print("\n");
print ( serialize($ary) );

print("\n\n---------------------------------------\n\n");

unset($obj); unset($obj1); unset($ary);

$obj->name = "Homer";
$ary[] = $obj;
$ary[] = $obj;

$obj1->name = "Homer";
$ary[] = $obj1;

$ary[1]->name = "Marge";

print_r($ary);
print("\n");
print ( serialize($ary) );

?>


Expected result:
----------------
I would expect to *not* see any indications of references (i.e. "r:2") in
the serialized output.

Actual result:
--------------
Array
(
    [0] => stdClass Object
        (
            [name] => Homer
        )

    [1] => stdClass Object
        (
            [name] => Homer
        )

    [2] => stdClass Object
        (
            [name] => Homer
        )

)

a:3:{i:0;O:8:"stdClass":1:{s:4:"name";s:5:"Homer";}i:1;r:2;i:2;O:8:"stdClass":1:{s:4:"
name";s:5:"Homer";}}

---------------------------------------

Array
(
    [0] => stdClass Object
        (
            [name] => Homer
        )

    [1] => stdClass Object
        (
            [name] => Marge
        )

    [2] => stdClass Object
        (
            [name] => Homer
        )

)

a:3:{i:0;O:8:"stdClass":1:{s:4:"name";s:5:"Homer";}i:1;O:8:"stdClass":1:{s:4:"name";s:
5:"Marge";}i:2;O:8:"stdClass":1:{s:4:"name";s:5:"Homer";}}


-- 
Edit bug report at http://bugs.php.net/?id=33207&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=33207&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=33207&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=33207&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=33207&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=33207&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=33207&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=33207&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=33207&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=33207&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=33207&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=33207&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=33207&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=33207&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=33207&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=33207&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=33207&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=33207&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=33207&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=33207&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=33207&r=mysqlcfg

Reply via email to