I have the following code that shows PHP overwrites Array elements of objects with the last iteration of the object. Why would it do such a thing? I want to keep a record of what happened to the object and I use an array to do this. Look at the results of the array as I change the object. Why does PHP do this. It seems to me like it could lead to serious errors. Even using unset() as recommended by others does not work. It would only work if I unset the ENTIRE object. But why would I want to do that if only 1 element has changed? If my object has 10 elements in it, and 1 changes, I need to copy the ENTIRE object into an array. The reasoning is irrelevant as to why I need to do that. I am more interested in finding out why PHP has this behavior. Is this a known bug?
<?php
$server->server = 'c17d7ae1-c298-4a1d-b7ee-2a3c9a2ab14d';
$servers['test1'] = $server;
print_r($servers);
unset($server->server); // <----- WHY DOES THIS NOT WORK?
$server->server = 'f88043af-dcf6-4802-a3d7-91cb594da4b0';
$servers['test2'] = $server;
print_r($servers);
?>
RESULTS:
php test.php
Array
(
[test1] => stdClass Object
(
[server] => c17d7ae1-c298-4a1d-b7ee-2a3c9a2ab14d
)
)
Array
(
[test1] => stdClass Object
(
[server] => f88043af-dcf6-4802-a3d7-91cb594da4b0 <--- This right here is WRONG
)
[test2] => stdClass Object
(
[server] => f88043af-dcf6-4802-a3d7-91cb594da4b0
)
)