different explanation

php

Solution

Not sure why this is the way it works, but, if you remove the `&` in front of `$this` while assigning it to your global variable, it will work.

To illustrate that, the following portion of code :

$global_obj = null;
class my_class
{
   public $my_value;
   public function __construct()
   {
        global $global_obj;
        $global_obj = $this;
   }
}
$a = new my_class;
$a->my_value = 5;

$global_obj->my_value = 10;
echo $a->my_value;

Gives the following output :

10

Here are the differences with your code :

- I remove the `&` before `$this` : with PHP 5, there is no need for that, when working with objects

- I translated the code to real PHP 5 :

- `__construct` for the constructor

- use `public`/`protected`/`private`, and not `var` for properties

As a sidenote, the code you posted should have given you the following warning :

Strict standards: Creating default object from empty value

Notes :

- I'm using PHP 5.3.2

- `E_ALL` doesn't include `E_STRICT` (source)

EDIT after some more searching :

Going through the References Explained section of the PHP manual, and, more specifically the What References Do page, there is a warning given that says (quoting) :

If you assign a reference to a variable declared global inside a function, the reference will be visible only inside the function. You can avoid this by using the `$GLOBALS` array.

And there is an example going with it.

Trying to use `$GLOBALS` in your code, I have this portion of code :

$global_obj = null;
class my_class
{
   public $my_value;
   public function __construct()
   {
        $GLOBALS['global_obj'] = & $this;
   }
}
$a = new my_class;
$a->my_value = 5;

$global_obj->my_value = 10;
echo $a->my_value;

And I get the following output :

10

Which seems to work ;-)

If I replace the `__construct` method by this :

public function __construct()
{
    global $global_obj;
    $global_obj = & $this;
}

It doesn't work...

So it seems you should not use `global`, here, but `$GLOBALS`.

The explanation given in the manual is :

Think about `global $var;` as a shortcut to `$var =& $GLOBALS['var'];`. Thus assigning another reference to `$var` only changes the local variable's reference.

And, just so it's said : using global variables is generally not quite a good idea -- and, in this specific situation, it feels like a very bad idea...

(Now, if this question what just to understand why... Well, I can understand your curiosity ;-) )

Problem

The code ``` $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; ``` echoes 5, not 10. "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." I still don't understand it, so can anyone please explain it differently, and help me understand?

Original source