#43983 [Opn->Bgs]: Weird referencing with stdClass objects
ID: 43983
Updated by: jani@php.net
Reported By: rubens21 at gmail dot com
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
PHP Version: 5.2.5
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2008-04-07 19:02:45] 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-02-24 20:17:54] 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-02-24 19:44:21] 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-23 17:04:12] rubens21 at gmail dot com
"What does it output when you replace print_r() with var_dump() ?"
R: The same
------------------------------------------------------------------------
[2008-02-01 22:28:15] jani@php.net
What does it output when you replace print_r() with var_dump() ?
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/43983
--
Edit this bug report at http://bugs.php.net/?id=43983&edit=1
Thread (8 messages)