The reference of PHP (that is, adding the & symbol in front of a variable, function, object, etc.), the 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 reference
PHP reference allows you to use two variables to point to the same content
2. Function transfer by reference (call by address)
I won’t go into details about call by address. The code will be given directly below
function test(&$a)
{
$a=$a+100;
}
$b=1;
echo $b;//Output 1
test($b); //What $b is passed to the function here is actually the memory address where the variable content of $b is located. The value of $b can be changed by changing the value of $a in the function
echo "
";
echo $b;//Output 101
?>
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 ampersand in front of $b in the above "test($b);", but in the function "call_user_func_array", if you want to refer to the passed parameters, you need the ampersand, as shown in the following code:
Explanation below:
What you get in this way $a=test(); is not actually a reference return of the function. It is no different from an ordinary function call. The reason: This is the regulation of PHP
PHP It is stipulated that what is obtained through $a=&test(); is the reference return of the function
As for what is a reference return (the PHP manual says: reference return is used when you want to use a function to find where the reference should be bound) When the variable is above) I didn’t understand this nonsense for a long time
Using the above example to explain,
calling a function using $a=test() only assigns the value of the function to $a, and any changes to $a will not affect $b in the function.
When calling a function through $a=&test(), its function is to point the memory address of the $b variable in return $b and the memory address of the $a variable to the same place
That is, Equivalent to this effect ($a=&$b;) So changing the value of $a also changes the value of $b, so after executing
$a=&test();
$a=5;
Afterwards, the value of $b becomes 5
Static variables are used here to let everyone understand the reference return of functions. In fact, the reference return of functions is mostly used in objects
Attached is another official PHP example:
4. Object reference
The above code is the running effect 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.
As of PHP 5, new automatically returns a reference, so using =& here is obsolete and produces an E_STRICT level message.
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 for this anonymous object copy. 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:
$baz =& $bar;
print( $bar );
print("n");
print("Now Creating MasterOne and Two and passing $bar to both constructorsn");
$m1 = new MasterOne( $bar );
$m2 = new MasterTwo( $bar );
print( $m1 );
print( $m2 );
print("n");
print("Now changing value of $bar and printing $bar and $bazn");
$bar->setName('baz');
print( $bar );
print( $baz );
print("n");
print("Now printing again MasterOne and Twon");
print( $m1 );
print( $m2 );
print("n");
print("Now changing MasterTwo's foo name and printing again MasterOne and Twon");
$m2->setFooName( 'MasterTwo's Foo' );
print( $m1 );
print( $m2 );
print("Also printing $bar and $bazn");
print( $bar );
print( $baz );
?>
输出:
Analysis of the previous example:
$bar in instance objects $m1 and $m2 is a reference to instance $bar, not a copy. This is a feature of object references in php5, that is to say
1. Inside $m1 or $m2 , any operation on $bar will affect the related value of the external object instance $bar.
2. Changes to the external object instance $bar will also affect the reference values of $bar inside $m1 and $m2.
In php4, when you want to use an object instance to refer to the properties of another object as mentioned above, the equivalent code (i.e. reference call) is similar to the following:
5. The role of references
If the program is relatively large, there are many variables referencing the same object, and you want to manually clear the object after using it, I personally recommend using the "&" method, and then use $var=null method to clear. 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.
6. 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:
Think of global $var; as shorthand for $var =& $GLOBALS['var'];. Thus assigning another reference to $var only changes the reference to the local 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. 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.
In layman terms
1: If there is the following code
[code]
$a="ABC";
$b=&$a;
?> ;
In fact, $a and $b point to the same memory address at this time instead of $a and $b occupying different memories
2: If you add the following code to the above 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 an ampersand in front of variables, functions, objects, etc.) is an advanced topic. Newbies should pay attention. It is important to correctly understand references in PHP, which has a great impact on performance, and misunderstanding may lead to program errors!
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:
}
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. .