different explanation
Posted
by Delirium tremens
on Stack Overflow
See other posts from Stack Overflow
or by Delirium tremens
Published on 2010-04-24T22:38:40Z
Indexed on
2010/04/24
22:43 UTC
Read the original article
Hit count: 262
php
The following code echoes 5, not 10:
$global_obj = null;
class my_class
{
var $value;
function my_class()
{
global $global_obj;
$global_obj = &$this;
}
}
$a = new my_class;
$a->my_value = 5;
$global_obj->my_value = 10;
echo $a->my_value;
"Upon first examination, it would seem that the constructor of my_class stores a reference to itself inside the $global_obj variable. Therefore, one would expect that, when we later change the value of $global_obj->my_value to 10, the corresponding value in $a would change as well. Unfortunately, the new operator does not return a reference, but a copy of the newly created object."
Please, give me a different explanation.
© Stack Overflow or respective owner