Object Assignment in PHP

Firstly, sorry for the stupid question, but I read the article in php.net and I could not understand what exactly this says.

<?php class SimpleClass { // property declaration public $var = 'a default value'; // method declaration public function displayVar() { echo $this->var; } } ?> <?php $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); ?> 

And this outputs this:

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

$ instance and $ reference point to the same place, I get this, and I understand why we get NULL and NULL for them.

But what about $? Does this also indicate where the $ instance is stored? Why, when we use $instance->var , does it affect $ assign, but when we set the value of $ instance to null, is any change assigned to $?

I thought that all three variables point to one place in memory, but obviously I'm wrong. Could you explain to me what exactly is happening and what is assigned? Thank you very much!

+7
source share
7 answers

$reference points to the value of $instance , which in itself is a reference to an object. Therefore, when you change the value contained in $instance , $reference reflects this change.

$assigned , on the other hand, is a copy of the value of $instance and independently points to the object itself, to which $instance refers. Therefore, when the value of $instance updated to indicate nothing (i.e. null ), $assigned does not affect, because it still points to the object and does not care about the contents of $instance .

+6
source

Setting $instance = null; does not update $assigned because it does not update the data at the place where $instance points. Instead, it changes the $instance pointer itself, so that the pointer no longer points to the same data as $assigned .

+4
source

An object is a separate object in memory that can have 1 or more references. If it got 0, this is garbage collection.

The variable containing the object actually points to (refers to) this object. Therefore, when you do this:

 $var1 = new Object(); $var2 = $var1; 

you simply create two variables pointing to the same object ($ var2 copies the value of $ var1, but this value is a reference to the object).

If you then write, $var1 = null , you will remove this link, but $ var2 still points to an object.

The operator and actually returns a reference to the variable behind it, so when you write

 $var1 = &var2; 

then $ var1 does not point to an object, which also includes $ var2, but $ var1 points to $ var2. Therefore, when you make $ var2 null, $ var1 points to the same zero.

+2
source

In the next line, we reserve the memory location in which the new object is stored, and a pointer to it, designated as $instance :

 $instance = new SimpleClass(); 

In the next line, we create a reference to the $instance pointer, marked as $reference :

 $reference =& $instance; 

In the next line, we reserve a new memory cell, designated as $assigned , different and independent of the one indicated above, in which we store a copy of the pointer (and not the object itself) created above:

 $assigned = $instance; 

By setting $instance to null , you only disable the pointer to the memory cell that contains the actual object, not the object itself. Any other links to it (for example, $reference ) will also be canceled.

$assigned is an independent copy of a pointer (stored in another memory location) that still points to the actual object.

That is why it can still be used to refer to the actual object.

This comment, found in the PHP documentation, supports what is said above:

 <?php class A { public $foo = 1; } $a = new A; $b = $a; $a->foo = 2; $a = NULL; echo $b->foo."\n"; // 2 $c = new A; $d = &$c; $c->foo = 2; $c = NULL; echo $d->foo."\n"; // Notice: Trying to get property of non-object... ?> 

Note

This is a peculiar exception when it comes to memory management. If copying a variable or array is only addressed using the = operator, you must explicitly use the clone keyword with objects, as indicated in the PHP documentation:

An exception to the usual assignment by behavior value in PHP occurs with objects that are referenced in PHP 5. Objects can be explicitly copied using the clone keyword.

+2
source

The short answer is when you create a link using & , this is what happens:

 $A ===+ |===> [symbol table entry x] ====> (memory location) $B ===+ 

If after that you complete the usual task:

 $A ===+ |===> [symbol table entry x] ===> (memory location) $B ===+ ^ | $C =======> [symbol table entry y] ========+ 

Skip the code:

 $instance = new SimpleClass(); $assigned = $instance; 

Statement:

 $assigned === $instance 

Both $assigned and $instance refer to the same SimpleClass instance.

 $reference =& $instance; 

Statement:

 $assigned === $instance === $reference 

All variables refer to the same instance.

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

Statement:

 $assigned === $instance === $reference 

Although the $instance property has been changed, it does not affect links.

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

Since $reference is just an alias in the character table, it gets the same value as $instance ; the reference counter of the source instance (i.e. memory location) is reduced to 1, the variable $assigned .

+1
source

As you can see in the code

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

The $ instance has now become null, but before that $ assign was set. therefore $ assign did not get a null value. $ reference got a null value because you referenced it with = & it stores the value of $ instance.

0
source

It is very simple:

 $a = 3; $b = $a; $c = &$a; $a = 0; var_dump($a); var_dump($b); var_dump($c); 

The output will be:

 int(0) int(3) int(0) 

Thus, this is not only the purpose of the object, but also the normal purpose.

0
source

All Articles