Home  >  Article  >  Backend Development  >  PHP reference

PHP reference

巴扎黑
巴扎黑Original
2016-11-23 13:15:11971browse

The so-called reference in PHP means that different names access the same variable content. It can be used on variables, functions and objects by adding the ampersand in front of them. Let’s talk about the types and functions of references in detail:

1. Reference type

1.1. Variable reference: Two variables point to the same content

Php code

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


1.2. Function transfer reference

Php Code

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

It should be noted that if test(1); is used here, an error will occur. Think about the reason yourself

1.3. Function reference

Php code

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

is explained as follows:
$a=test Calling a function in the () method only assigns the value of the function to $a, and any changes to $a will not affect $b in the function. However, calling a function in the $a=&test() method has its role The memory address of the $b variable in return $b and the memory address of the $a variable point to the same place, which produces the equivalent effect ($a=&b;). Therefore, changing the value of $a also simultaneously The value of $b is changed, so after executing


$a=&test();
$a=5;


, the value of $b becomes 5

This is for the convenience of understanding the reference return of the function Only static variables are used. In fact, function reference returns are mostly used in objects

1.4, object references

Php code

class testa  
{  
    var $abc="ABC";   
}  
  
$b=new testa;  
$c = &$b; // or $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, object copying This is achieved through references. In the above example, $b=new a; $c=$b; is actually equivalent to $b=new a; $c=&$b;
The default in PHP5 is to call objects by reference, but sometimes you may want to create a A copy of the object, and hopes that changes to the original object will not affect the copy. For this purpose, PHP defines a special method called __clone.

2. The role of references


If the program is relatively large, there are many variables referencing the same object, and you want to manually To clear it, I personally recommend using the "&" method, and then using $var=null to clear it. At other times, 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.

3. 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:

<?php 
   $a = 1; 
   $b =& $a; 
   unset ($a); 
?>

will not unset $b, but only destroy $a.

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

<?php 
    $var =& $GLOBALS["var"]; 
?>

This means that, for example, unset $var will not unset a global variable.

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

PS:

One advantage of PHP is that it can take advantage of its automatic garbage collection mechanism (release memory). That is, you don't need to do any processing to release the memory after using the variables, PHP will do it for you. Of course, we can call the unset() function to free the memory if we want, but generally we don't need to do this.


However, in PHP, there is at least one situation where the memory will not be released automatically, as follows:


If there is a mutual reference relationship between two objects, such as "parent object-child object", for Calling unset() on the parent object will not release the memory referencing the parent object in the child object (even if the parent object is garbage collected).


At this time, even if unset() is called manually. Details can be found at: http://bugs.php.net/bug.php?id=33595.

The compromise is to call the __destruct() method in the object in the unset() function;


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

Addition:


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, php The reference adopts the principle of "copy-on-write", that is, unless a write operation occurs, variables or objects pointing to the same address will not be copied.

In layman’s terms
1: If you have the following code

$a="ABC";
$b=$a;

In fact, at this time, $a and $b both point to the same memory address, not $a and $b occupy different memories

2: If you add the following code to the above code

$a="EFG";

Since the data in the memory pointed to by $a and $b needs to be re- After writing once, the Zend core will automatically judge at this time, 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