0

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
        )

)
2
  • 1
    "Even using unset() as recommended by others" --- recommended by others for what? Commented Feb 4, 2014 at 23:59
  • 1
    You're modifying the same object then wondering. You want a second - object - you create a second object. Commented Feb 5, 2014 at 0:00

3 Answers 3

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Clone is the correct fix. I was actually looking for an answer that would not use unset since it didnt seem correct to me. I was unaware of the clone type until now.
You don't need the unset at all. And while clonereally is a fix, it's nothing more - you still shouldn't do what you're doing there.
0

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);

Comments

0

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

4 Comments

No, that's not the problem here. $servers->server and $servers['server'] are completely equivalent when used on objects.
But $server->server and $servers['server'] are not: One has an s in the end.
Nasty variable names there ;) Agreed, not the same. Still not what I tihnk the OP wanted to archive. Goal seems to be to have a copy of the original value in $servers[0] after changing $server->server
Unfortunetly the naming convention is not my own and nothing I can control. The naming convention comes from elastichosts which uses 'server' element for their UUID field. Why its not called uuid, I dont know. But to replicate my issue as close as possible, I kept the naming convention in case it was an issue. Johannes is correct, clone is what I was looking for :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.