Home  >  Article  >  The true understanding of references & in PHP - variable references, function references, object references

The true understanding of references & in PHP - variable references, function references, object references

无忌哥哥
无忌哥哥Original
2018-06-27 15:49:471763browse

Variable reference
PHP reference allows you to use two variables to point to the same content

<?php
$a="ABC"; 
$b =&$a; 
echo $a;//这里输出:ABC 
echo $b;//这里输出:ABC 
$b="EFG"; 
echo $a;//这里$a的值变为EFG 所以输出EFG 
echo $b;//这里输出EFG 
?>

I won’t go into details about function call by address and call by address. The code is given directly below

<?php
function test(&$a){ 
    $a=$a+100; 
} 
$b=1; 
echo $b;//输出1 
test($b);   //这里$b传递给函数的其实是$b的变量内容所处的内存地址,通过在函数里改变$a的值 就可以改变$b的值了 
echo "<br>"; 
echo $b;//输出101
?>

It should be noted that if test(1); is used here, an error will occur. The reason is: PHP stipulates that the reference passed cannot be a constant (you can see the error message).

Function reference return First look at the code

<?php
function &test(){ 
    static $b=0;//申明一个静态变量 
    $b=$b+1; 
    echo $b; 
    return $b; }
}
$a=test();//这条语句会输出 $b的值 为1 
$a=5; $a=test();//这条语句会输出 $b的值 为2
$a=&test();//这条语句会输出 $b的值 为3 
$a=5; $a=test();//这条语句会输出 $b的值 为6
?>

Explain below: In this way $a=test(); what you get is not actually a function reference return, which is different from ordinary function calls There is no difference. As for the reason: This is the regulation of PHP. PHP stipulates that what is obtained through $a=&test(); is the reference return of the function. As for what is the reference return (the PHP manual says: the reference return is used when you want to use the function to find Which variable the reference should be bound to.) This nonsense made me unable to understand it for a long time

Use the above example to explain that the function is called in the $a=test() method, which just changes the function's The value is just assigned to $a, and any changes to $a will not affect $b in the function. When calling the function through $a=&test(), its function is to return $b in $b The memory address of the variable and the memory address of the $a variable point to the same place, which produces the same effect ($a=&b;). So changing the value of $a also changes the value of $b, so after executing $ a=&test(); $a=5; After that, the value of $b becomes 5

The static variable is used here to let everyone understand the reference return of the function. In fact, the reference return of the function is mostly used in objects. References to objects in

<?php
class a
{ 
   var $abc="ABC";
}
$b=new a; $c=$b; echo $b->abc;//这里输出ABC 
echo $c->abc;//这里输出ABC 
$b->abc="DEF"; 
echo $c->abc;//这里输出DEF
?>

The above code is the running effect in PHP5. In PHP5, the copying of objects is achieved through references. In the above column, $b=new a; $c=$b; is actually equivalent to $b=new a; $c=&$b; The default in PHP5 is to call the object by reference, but sometimes you may want to create an object A copy of the original object and hope that changes to the original object will not affect the copy. For this purpose, PHP defines a special method called __clone.

The role of reference
If the program is relatively large, If there are many variables that refer to the same object, and you want to clear it manually after using the object, I personally recommend using the "&" method, and then using $var=null to clear it. In other cases, use the default method of php5. In addition, For the transfer of large arrays in php5, it is recommended to use the "&" method, after all, it saves memory space.

Unreference When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the variable contents are destroyed. For example:

62fa166409ba982efc37866dbe39c389

will not unset $b, just $a. function quoteTest(){ global $var; //Equivalent to $var = &$GLOBALS['var']; unset($var); //Deletion only deletes the reference, but the referenced content still exists. Same as above. This does not mean The variable content is destroyed}$var=1;quoteTest();echo $var; // Result 1

-------------------------- -------------------------------------------------- --------------------------

Will not unset $b, just $a.

function quoteTest(){ global $var; //Equivalent to $var = &$GLOBALS['var']; $var = 5; //Because they both point to the same memory content}$var=1 ;quoteTest();echo $var; //Result 5--------------------------------------------- -------------------------------------------------- -------

'&' This is a reference

global reference When declaring a variable with global $var, a reference to the global variable is actually established. That is the same as doing:

1494836c6ffb3c5371a1f493f42472a1

This means, for example, unset $var Global variables will not be unset.

$this In a method of an object, $this is always a reference to the object that calls it.

//Here’s another little episode. The pointing (similar to pointer) function of the address in PHP is not implemented by the user himself, but is implemented by the Zend core. The reference in PHP uses "copy-on-write" The principle is that unless a write operation occurs, variables or objects pointing to the same address will not be copied.

Popular explanation 1: If there is the following code [php] $a="ABC"; $b=$a; [/php] In fact, $a and $b both point to the same memory address at this time It is not that $a and $b occupy different memories

2: If you add the following code to the above code [php] $a="EFG"; [/php] Since $a and $b The data in the memory pointed to by $b needs to be rewritten. At this time, the Zend core will automatically determine and automatically produce a data copy of $a for $b and re-apply for a piece of memory for storage.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn