(PHP learning notes) I have a reference to PHP variables, and the understanding of object references.


We can know that in PHP, the variable starts with a $ symbol, and then the name of the variable. Variables can be referenced to one content, and PHP references allow you to point to the same content with two variables.

$a = 1;          
$b = $a;       
echo '$a ='.$a.'; '.'$b = '.$b."\n".
$b = 2;
echo '$a ='.$a.'; '.'$b = '.$b."\n". 
Output result:

$ A = 1; $ b = 1; $ a = 1; $ b = 2;
   $ A assigns $ b, copy one copy The variable on the right will generate a memory space, and the result may be the same content in memory. At this time, the variable A and the variable B also point to the same content "1", so the output result is "1". When the "$ 2" statement is executed, the variable B is changed to the content "2", the original variable A has no effect, but the point of variable B has changed, so the output result variable A has no change, the variable B is "2". 
Next, the example code is as follows:

Output result:
$c = 1;
$d = &$c;
echo '$c ='.$c.'; '.'$d = '.$d."\n".
$d = 2;
echo '$c ='.$c.'; '.'$d = '.$d."\n". $ c = 1; $ b = 1; $ c = 2; $ b = 2; 

$ c will assign the value to $ D, add “&” symbols, symbol “&” to reference. With reference, the PHP engine will not copy a variable, in fact, point the pointer to the address of $ A in memory, and this pointer is saved. Modifications to any of them will affect other variables, and delete one of these variables, others will be active.
   Object reference 

When describing the reference object, insert a concept (destructor): The destructor will be deleted in all references to an object or execute when the object is explicitly destroyed. , Release the memory occupied by the object. Let’s take a look at the sample code:

case 1:

Name; Echo $ C-> Name; $ b-> name = “bob”; Echo $ C-> Name; $ b = null; $ c = null; // This time the destructor is the destructor? >


    Name; ECHO $ ​​C-> Name; $ b-> name = "bob"; Echo $ C-> Name; $ B = NULL; // At this time, the destructor is the destructor?> 
Output result:

From the case of the case, it can be seen that $ b and $ c can call the properties in the function A, and some of them can make changes to the content content, when $ b and $ c are empty. However, there is no variable in the reference object, and the destructor is called. In case 2, add the & symbol when the variable is assigned, meaning that a $ C equivalent to $ B is copied, both is equal. When one of them is empty, the other party will also be empty, and the destructor is called.


© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment