About & quotation for beginners in PHP
Official documentation:
1. What is the reference: http://www.php.net/manual/zh/language.references.whatare.php
2. What does the reference do: http://www.php.net/manual/zh/language.references.whatdo.php
PHP reference (that is, adding an ampersand in front of a variable, function, object, etc.)
Reference in PHP means: different names access the same variable content.
It is different from pointers in C language. The pointer in C language stores the content of the variable and the address stored in the memory.
1. Variable references
PHP references allow you to use two variables to point to the same content
2. Function reference transfer (call by address)
I won’t go into details about the call by address. The code will be given directly below
function test(&$a)
It should be noted that if test(1); is used here, an error will occur. You have to think about the reason yourself.
Note:
Do not add the & symbol in front of $b in the above "test($b);", but in the function "call_user_func_array", if you want to quote the parameters, you need the & symbol, as follows The code is shown:
3. Function reference returns
Look at the code first
function &test()
The following explanation:
What you get in this way $a=test(); is not actually a function reference return, which is no different from an ordinary function call. As for the reason: This is the regulation of PHP
PHP stipulates that the reference return of the function is obtained through $a=&test();
As for what is a reference return (the PHP manual says: Reference return is used when you want to use a function to find a reference that should be bound When it is on which variable.) This nonsense made me unable to understand it for a long time.
Using the above example to explain, it is to call the function in the way of
$a=test(), which just assigns the value of the function. Just give $a, and any changes to $a will not affect $b
in the function. If the function is called through $a=&test(), its function is to return the $b variable in $b The memory address of the variable $a points 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;
, the value of $b becomes 5
This is to let everyone understand the reference return of the function. When using static variables, in fact, function reference returns are mostly used in objects
Attached is an official php example:
This is the way how we use pointer to access variable inside the class.
4. Object reference
class a{
The above code is the effect of running in PHP5
In PHP5, object assignment is a reference process. 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 objects by reference, but sometimes you may want to Create a copy of an object and hope that changes to the original object will not affect the copy. For this purpose, PHP5 defines a special method called __clone.
In php4, object assignment is a copy process,
For example: $b=new a, where new a produces an anonymous a object instance, and $b at this time is a copy of this anonymous object. In the same way, $c=$b is also a copy of the content of $b. Therefore, in php4, in order to save memory space, $b=new a will generally be changed to the reference mode, that is, $b=& new a.
Here’s another official example:
In php5, you don’t need to add anything else to achieve the “object reference” function:
class foo{
输出:
Only Created $bar and printing $bar上个例子解析:
在php4中,要实现如上述的 用一个对象实例去当着另外一个对象的属性时,其等价代码(即引用调用)类似如下:
class foo{
5.引用的作用
如果程序比较大,引用同一个对象的变量比较多,并且希望用完该对象后手工清除它,个人建议用 "&" 方式,然后用$var=null的方式清除. 其它时候还是用php5的默认方式吧. 另外, php5中对于大数组的传递,建议用 "&" 方式, 毕竟节省内存空间使用。
6.取消引用
当你 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。例如:
不会 unset $b,只是 $a。
7.global 引用
当用 global $var 声明一个变量时实际上建立了一个到全局变量的引用。也就是说和这样做是相同的:
这意味着,例如,unset $var 不会 unset 全局变量。
如果在一个函数内部给一个声明为 global 的变量赋于一个引用,该引用只在函数内部可见。可以通过使用 $GLOBALS 数组避免这一点。
Example Reference global variables within a function
$var1 = "Example variable";
8.$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, and is referenced in PHP. It is 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 terms
1: If there is the following code
In fact, $a and $b point to the same memory address at this time, rather than $a and $b occupying different memories
2: If you add The following code
Since the data in the memory pointed to by $a and $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
PHP references (that is, adding the ampersand in front of variables, functions, objects, etc.) is an advanced topic. Newbies should pay attention. It is very important to correctly understand PHP references, which has a great impact on performance, and misunderstandings may lead to Program error!
Many people misunderstand that references in PHP are the same as pointers in C. In fact, they are not, and they are very different. Except for the pointers in C language that do not need to be explicitly declared during the array transfer process, other points need to be defined using *. However, the pointer to address (similar to a pointer) function in PHP is not implemented by the user himself, but is implemented by the Zend core. Yes, the reference in PHP 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, such as the following code:
$a = array('a','c'...'n');
If the program only executes here, $a and $b are the same, but they do not occupy different memory spaces like C. Instead, they point to the same memory. This is php and c. The difference is that you don’t need to write $b=&$a to mean that $b points to the memory of $a. zend has already implemented the reference for you, and zend will be very smart to help you judge when to do this and when. It shouldn't be handled this way.
If you continue to write the following code later, add a function, pass parameters by reference, and print out the array size.
function printArray(&$arr) // Pass by reference
In the above code, we pass the $a array into the printArray() function by reference. The zend engine will think that printArray() may cause changes to $a, and will automatically produce an $a for $b. Copy the data and re-apply a piece of memory for storage. This is the "copy-on-write" concept mentioned earlier.
If we change the above code to the following:
function printArray($arr) //Value transfer
The above code directly passes the $a value to printArray(). There is no reference transfer at this time, so there is no copy-on-write.
You can test the execution efficiency of the above two lines of code. For example, add a loop outside 1000 times and see how long it takes to run. The results will let you know that incorrect use of references will cause performance to drop by more than 30%.
Self-understanding: When passing by value, it has nothing to do with the parameters in the function, which is equivalent to the role of local variables, but when passing by address (reference), it is related to the parameters within the function, which is equivalent to the role of global variables. From a performance perspective, it is enough to look at the above analysis. .