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 4246690
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:00:27+00:00 2026-05-21T04:00:27+00:00

I am learning OOP’s PHP by reading the php offical documents first: class SimpleClass

  • 0

I am learning OOP’s PHP by reading the php offical documents

first:

class SimpleClass
  {
      // property declaration
      public $var = 'a default value';

      // method declaration
      public function displayVar() {
      echo $this->var;
      }
  }

Second:

  $instance = new SimpleClass();

  $assigned   =  $instance;
  $reference  =& $instance;

  $instance->var = '$assigned will have this value';

  $instance = null; // $instance and $reference become null

  var_dump($instance);
  var_dump($reference);
  var_dump($assigned);

Result:

 NULL
  NULL
  object(SimpleClass)#1 (1) {
    ["var"]=>
      string(30) "$assigned will have this value"
  }

This example I dont understand about assgined by reference and value

here .even I see the result output.But I still not get an idea.

My question is why $instance and $reference become null?

  • 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-21T04:00:28+00:00Added an answer on May 21, 2026 at 4:00 am

    My question is why $instance and $reference become null?

    PHP references operate in a funny way. I’m going to break out a real-life analogy.

    Imagine you have a cup. The cup is full of water.

    The cup is a variable. The water is the contents of the variable.

    Any references you create point at the cup, not the water.

    If you replace the water in the cup with coffee, then all of the references to that cup suddenly contain the same coffee.

    And when you empty the cup out, all of the references to the cup are suddenly empty.

    Now, objects are quite fun. You see, when you assign them to variables, it actually does so kind of by reference. But objects-by-ref aren’t normal references. From the PHP interactive prompt:

    php > class Foo { public $bar; }
    php > $f = new Foo();
    php > $f->bar = 1;
    php > $f_copy = $f;
    php > $f_copy->bar = 2;
    php > echo $f->bar;
    2
    

    Not what you expected, right? It gets funnier.

    php > $f_copy = $f;
    php > $copy_ref = &$f_copy;
    php > $copy_ref->bar = 3;
    php > echo $f->bar;
    3
    

    That is a bit more expected. But is this?

    php > $f_copy = null;
    php > print_r($copy_ref);
    php > var_export($copy_ref);
    NULL
    php > print_r($f);
    Foo Object
    (
        [bar] => 3
    )
    

    As you can see, even though we completely emptied out the variable containing the reference to our object, the original object was untouched, even though that object was placed into the copy by reference!

    Welcome to PHP, where things like this are normal. I suggest frequent checks of your own sanity.

    Edit: Oh, one more thing. If you need to make a real copy of the object, you have to clone it:

    php > $not_f = clone $f;
    php > $not_f->bar = 'I am not $f';
    php > echo $f->bar;
    3
    php > echo $not_f->bar;
    I am not $f
    

    It also works on refs:

    php > $not_f_copy = $not_f;
    php > $not_copy_ref = &$not_f_copy;
    php > $headsplode = clone $not_copy_ref;
    php > $not_f->bar = 'No, I am Spartacus!';
    php > echo $headsplode->bar;
    I am not $f
    

    Oh, and before someone asks: In some languages, references work at the value level, not the variable level. Perl is a good example here. If you create a reference to a variable, then reassign the original, the reference still contains the original value. I came to PHP through Perl, and this seemingly small difference drove me up the wall.

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

Sidebar

Related Questions

I am learning OOP with PHP. I am creating a class to extract XML
I'm on the stage of learning OOP with the help of PHP and David
I'm currently learning php, and I'm testing out oop and classes. My code is:
I'm learning Java (and OOP) and although it might irrelevant for where I'm at
I remember first learning about vectors in the STL and after some time, I
I am learning OOP with the pascal programming language.After googling the Internet, I found
Im new in OOP programming and C++ and Im currently into learning design patterns.
I'm learning how to make OOP with JavaScript . Does it have the interface
I was wondering how to do the PHP OOP design pattern for real life
I'm learning OOP in Java, but I'm not sure how to implement this reference

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.