php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43983 Weird referencing with stdClass objects
Submitted: 2008-01-30 15:37 UTC Modified: 2008-07-15 11:05 UTC
Votes:3
Avg. Score:3.7 ± 1.9
Reproduced:0 of 1 (0.0%)
From: rubens21 at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.5 OS:
Private report: No CVE-ID: None
 [2008-01-30 15:37 UTC] rubens21 at gmail dot com
Description:
------------
change atribute of a class, and arrays that received values is change too.
I know that the bug #33207 (http://bugs.php.net/bug.php?id=33207&edit=2) describes this same problem, but there is not the solution and the id of the other related.

Reproduce code:
---------------
$test = new stdClass();
$Objeto = new stdClass();

$test->valor = "No Change!";
$Objeto->valorDeTeste[] = $test;

$test->valor = "Yes, change!";
$Objeto->valorDeTeste[] = $test;

print_r($Objeto->valorDeTeste);

Expected result:
----------------
Array
(
    [0] => stdClass Object
        (
            [valor] => No Change!
        )

    [1] => stdClass Object
        (
            [valor] => Yes, change!
        )

)

Actual result:
--------------
Array
(
    [0] => stdClass Object
        (
            [valor] => Yes, change!
        )

    [1] => stdClass Object
        (
            [valor] => Yes, change!
        )

)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-30 15:40 UTC] rubens21 at gmail dot com
This example is more simple:

$test = new stdClass();
$Objeto = new stdClass();

$test->valor = "The first value";
$Objeto->valorDeTeste[] = $test;

$test->valor = "The second value";
print_r($Objeto->valorDeTeste);


Expected:
Array
(
    [0] => stdClass Object
        (
            [valor] => The first value
        )

)

Actual

Array
(
    [0] => stdClass Object
        (
            [valor] => The second value
        )

)
 [2008-02-01 22:28 UTC] jani@php.net
What does it output when you replace print_r() with var_dump() ?
 [2008-02-23 17:04 UTC] rubens21 at gmail dot com
"What does it output when you replace print_r() with var_dump() ?"

R: The same
 [2008-02-24 19:44 UTC] jani@php.net
That's impossible, the output of var_dump() differs a LOT from print_r(). So can you please really test it?
 [2008-02-24 20:17 UTC] rubens21 at gmail dot com
I'm sorry, 'The same' = 'The same problem'

CODE:
$test = new stdClass();
$Objeto = new stdClass();

$test->valor = "The first value";
$Objeto->valorDeTeste[] = $test;

$test->valor = "The second value";

echo "<pre>";
print_r($Objeto->valorDeTeste);
echo "<br />";
var_dump($Objeto->valorDeTeste);
echo "</pre>";








Array
(
    [0] => stdClass Object
        (
            [valor] => The second value
        )

)

array(1) {
  [0]=>
  object(stdClass)#1 (1) {
    ["valor"]=>
    string(16) "The second value"
  }
}
 [2008-04-07 19:02 UTC] ben at mailinator dot com
How is this weird?  $test is an object.  You store a reference to $test, 
and then you modify $test.  Since you only stored a reference to the 
original object, of course you will see the new value when you print out 
the reference.

This changed between PHP4 and PHP5.  PHP4 stores a copy rather than a 
reference.  If you are looking for the PHP4 behavior, you can use the 
"clone" keyword.  e.g. $Objeto->valorDeTeste[] = clone $test;
 [2008-07-15 11:05 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 21:01:30 2024 UTC