Same object property with different values

user3810691

I have a problem occurring in one function, for me it's strange because I thought that the behaviour of objects were different. The function is:

function getMessages($imapCon)
{
    $messageHeaders = array();
    $tempObj = new stdClass();
    $totalMessages = imap_num_msg($imapCon);
    for ($i = $totalMessages; $i > 0; $i--) 
    {
        $headers = imap_headerinfo($imapCon, $i);
        $tempObj->Unseen = $headers->Unseen;
        $tempObj->fromaddress = $headers->fromaddress;
        $tempObj->Date = $headers->Date;
        $tempObj->Subject = $headers->Subject;
        $tempObj->uid = imap_uid($imapCon, $i);
        array_push($messageHeaders, $tempObj);
    }       
    return json_encode($messageHeaders);
}

In the json encoded, I get the same values for all properties (Unseen, fromaddress, Date...). The properties are set correct, but the values are duplicated. Why? If I do something like:

    for ($i = $totalMessages; $i > 0; $i--) 
    {
        $tempObj = new stdClass();
        ...
        array_push($messageHeaders, $tempObj);
        unset($tempObj);
    }       
    return json_encode($messageHeaders);
}

declaring the object inside the for, it works. But I believe this is a poor fix and nor the right thing to do...

astax

Creating a new $tempObj inside the loop is the right way. When you add this object into array, it's not copied, but a pointer to it is added into the array. Check this documentation - http://php.net/manual/en/language.oop5.basic.php#example-191 (look at the simple assignment, ignore the $reference variable for now).

So you end up with array containing the whole bunch of links pointing to the same object. But if you create a new $tempObj inside the loop, all objects will be different.

Alternatively, you can do this:

array_push($messageHeaders, clone($tempObj));

but although this will equally work in this case, it may not work if the $tempObj would have more complex structure, such as instances of othe classes as properties of $tempObj.

On a side note, in PHP4 your code would work as you expected as PHP4 was copying actual objects on assignment instead of just pointers as PHP5 does.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Different property list for a type and an instant of the same object?

From Dev

How to generate same object with different values into an ArrayList?

From Dev

Why GetDC returns different values for the same object?

From Dev

Flex items with same property values are rendering in different sizes

From Dev

Is it possible to have 2 different values for the same property in CSS?

From Dev

Is it possible to have 2 different values for the same property in CSS?

From Dev

Update the same property of every document of a mongoDb collection with different values

From Dev

How to get property values from same object in javascript?

From Dev

How to copy values from one property to another in the same object?

From Dev

Is it undefined behavior to have two pointers with different values referring to the same object?

From Dev

Two size() calls to the same object returning different values

From Dev

Accessing an object property in the same object

From Dev

Why different object share the same array if Ember Object with extend array property

From Java

Sorting object property by values

From Dev

Same object in different classes?

From Dev

Different types of the same object

From Dev

Php json_encode returning different values on 2 different servers for same object

From Java

How to assign values of object type attributes to different object type with same attribute properties in plsql?

From Dev

Ng-repeat - filter out by object's constant property with different values

From Dev

Java same enum with different values

From Dev

Passing different values to the same directives

From Dev

Addition with the same prompt, but different values

From Dev

same key with different values dictionary

From Dev

How to sum property values of an object?

From Dev

Count property values of javascript object

From Dev

object property values for individuals in Jena

From Dev

How to sum property values of an object?

From Dev

Push one object property with the same object in javascript

From Dev

Show same columns with different values in same row

Related Related

  1. 1

    Different property list for a type and an instant of the same object?

  2. 2

    How to generate same object with different values into an ArrayList?

  3. 3

    Why GetDC returns different values for the same object?

  4. 4

    Flex items with same property values are rendering in different sizes

  5. 5

    Is it possible to have 2 different values for the same property in CSS?

  6. 6

    Is it possible to have 2 different values for the same property in CSS?

  7. 7

    Update the same property of every document of a mongoDb collection with different values

  8. 8

    How to get property values from same object in javascript?

  9. 9

    How to copy values from one property to another in the same object?

  10. 10

    Is it undefined behavior to have two pointers with different values referring to the same object?

  11. 11

    Two size() calls to the same object returning different values

  12. 12

    Accessing an object property in the same object

  13. 13

    Why different object share the same array if Ember Object with extend array property

  14. 14

    Sorting object property by values

  15. 15

    Same object in different classes?

  16. 16

    Different types of the same object

  17. 17

    Php json_encode returning different values on 2 different servers for same object

  18. 18

    How to assign values of object type attributes to different object type with same attribute properties in plsql?

  19. 19

    Ng-repeat - filter out by object's constant property with different values

  20. 20

    Java same enum with different values

  21. 21

    Passing different values to the same directives

  22. 22

    Addition with the same prompt, but different values

  23. 23

    same key with different values dictionary

  24. 24

    How to sum property values of an object?

  25. 25

    Count property values of javascript object

  26. 26

    object property values for individuals in Jena

  27. 27

    How to sum property values of an object?

  28. 28

    Push one object property with the same object in javascript

  29. 29

    Show same columns with different values in same row

HotTag

Archive