tags:

views:

69

answers:

1

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?

+5  A: 

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 :


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 ;-) )

Pascal MARTIN
well, he gave you a pretty thorough explanation in his update. (+1)
Mark
Mr. Martin, this is from a certification practice test book. I don't need a correction. I need to know why it's 5, not 10. They can use a different wording to test my knowledge.
Delirium tremens
Sorry, but I don't think I can do better than what I posted -- especially what I've added with my edit that points to the references sections of the manual : I think the explanation is precisely what I quoted from those pages *(the last quote in my answer should be the reason why your code is not working, I'd say)*
Pascal MARTIN
It's great! Accepted as answer! Thanks!
Delirium tremens
@Delirium why are you reading certification practice books for PHP4? This is not from Zend, is it?
Gordon
@Delirium : you're welcomee :-) ;;; the question asked by Gordon is an interesting one, btw
Pascal MARTIN
I thought a newer edition didn't exist. It's from Zend. Do certifications work?
Delirium tremens
new feature! comments can be edited! :-) Mark even wrote a comment about a deleted comment...
Delirium tremens
There is a PHP 5 certification ( see http://www.zend.com/en/services/certification/php-5-certification/ ) ; in fact, I don't think the PHP 4 certification exists anymore, as PHP 4 has reached its end of life -- is not even maintained anymore for security-related stuff ! ;;; what do you mean *"do certifications work ?"* ; if you mean "do they help me get a job ?", well, not sure there is a definitive answer ^^
Pascal MARTIN