Philip Hallstrom wrote:
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.
looks like a problem related to copy-on-change strategy php uses (that
is when a copy is made its only actually copied if and when you change the copy, until
then its a really reference. if I understand it correctly)
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";}}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php