php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33207 Strange copy on change error with arrays, objects, and serialize
Submitted: 2005-05-31 23:53 UTC Modified: 2005-06-02 00:36 UTC
From: phallstrom at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.3.11 OS: FreeBSD 4.9-RELEASE
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: phallstrom at gmail dot com
New email:
PHP Version: OS:

 

 [2005-05-31 23:53 UTC] phallstrom at gmail dot com
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";}}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-02 00:36 UTC] sniper@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 22 02:01:33 2024 UTC