PHP会覆盖数组元素吗?

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
        )

)

When assiging objects to another variable, PHP doesn't copy the object, it creates an reference. That means, two variables are now pointing at the same object.

In your code, you first set the property server. Afterwards, you create a reference to the object that $serverpoints to in $servers[0]. You then unset the property server- which does work fine. afterwards, you're setting it again - to a new value. Next you create another reference in $servers[1]. Now you got 3 variables all pointing to THE SAME OBJECT: $server, $servers[0] and $servers[0].

If you really want to do it that way (stdClasses shouldn't be used as dynamic objects, this is the javascript way of doing it and while valid in PHP it contradicts OOP paradigmn), use cloneto copy the objects:

$server->server = 'c17d7ae1-c298-4a1d-b7ee-2a3c9a2ab14d';
$servers['test1'] = clone $server;

When you do this

$servers['test1'] = $server;

you have saved a reference of $server into the $servers array at the location 'test1'. Nothing you do to $server will affect what happens to the reference in the array, because they are not the same thing. Here's what you do instead:

unset($servers['test1']->server);

unset() unsets a variable - a.o.t. unsetting the place, where a variable once came from.

So

$server->server = 'c17d7ae1-c298-4a1d-b7ee-2a3c9a2ab14d';
$servers['test1'] = $server;

unset($server->server);  //THIS DOES WORK - BUT IT IS NOT WHAT YOU WANT
unset($servers['test1']); //NEW: THIS DOES WHAT I THINK YOU WANT