Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 743763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:54:04+00:00 2026-05-14T08:54:04+00:00

The code $global_obj = null; class my_class { var $value; function my_class() { global

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:54:04+00:00Added an answer on May 14, 2026 at 8:54 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 487k
  • Answers 487k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's probably easier if you just toggle the class. var… May 16, 2026 at 8:25 am
  • Editorial Team
    Editorial Team added an answer A mutex provides mutually exclusive access to a resource; in… May 16, 2026 at 8:25 am
  • Editorial Team
    Editorial Team added an answer Your hunch is correct. A typical server setup can answer… May 16, 2026 at 8:25 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.