You are here: Very odd (bug??) with arrays, objects, and serialize (please read!) « PHP « IT news, forums, messages
Very odd (bug??) with arrays, objects, and serialize (please read!)

Posted by Philip Hallstrom on 05/27/05 22:06

Hi all -
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...

Should I submit this as a bug? Am I missing something obvious?

Here's the 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) );

?>

Here's the output:

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";}}

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация