Home > Article > Backend Development > Detailed explanation of using PHP reference types with caution_PHP Tutorial
Use PHP references with caution Reference types (Reference) are used in many computer languages and exist as a very powerful and practical feature. It has an implementation similar to a pointer, but behaves differently from a pointer.
For example, C++ references can allow different variables to point to the same object, while maintaining the direct use of dot to obtain object members, without the cumbersome use of dereference operator (*) and Pointer to Member operator (->). In Java and C#, references are directly used as the main type, and developers try to avoid using pointers.
Reference types have also been introduced in PHP. In terms of object assignment and transfer, it can basically be regarded as the same reference transfer as Java/C# (see Objects and references for details). But at the same time, it supports obtaining a reference to the content through the reference operator (&) on the basic type. However, in actual use, PHP's reference type has many problems due to the entire PHP design structure, causing unexpected results in the program.
Reference variables can be assigned new references
In C++, a reference type variable can only be assigned a reference value when it is defined, so as long as we trace the definition of the variable, we can know what the variable is operating on.
But PHP is different. The definition of variables is blurred in PHP, and variables can be used without defining them. So a variable can be assigned a reference value multiple times.
The code is as follows | Copy code | ||||||||
$y = 7;
|
The code is as follows | Copy code |
int(21) int(7) Int(7) It can be seen from the result that $x remains unchanged, but $z is changed into a reference to $y. It is equivalent to unsetting the $z variable first and then assigning a new value. $z = &$x; unset($z); $z = &$y; This is actually more reasonable logic. For example, in the code below, we do not get a "Reference refer to" similar to "Pointer point to a Pointer". a Reference)" are just multiple reference variables that refer to the same piece of content. $x = 21; $y = &$x; $z = &$y |
Referencing an array element will make the element a reference type
Retrieving a reference from a variable will not change the type of the original variable, but if it is an element in an array, it will also change the element to a reference type.
Before looking at the problem code, the first thing to point out is:
Array assignment always involves value copying. Use the reference operator to copy an array by reference.
That is to say, PHP's array assignment is a copy rather than a reference. The assignment process will create a new array and assign it to the assigned variable. Array operations on the new variable will not affect the contents of the original array variable.
The code is as follows | Copy code | ||||||||
$a = array(21, 7); $b = $a;$b[0] = 7; var_dump($a);
echo ' //Output: //array(2) { [0]=> int(21) [1]=> int(7) } //array(2) { [0]=> int(7) [1]=> int(7) } Let's take a look at what exceptions will occur if we reference elements in the array.
'; var_dump($b); echo ' '; var_dump($c); echo ' '; // Output: // array(2) { [0]=> &string(2) "21" [1]=> int(7) } // array(2) { [0]=> &string(2) "21" [1]=> string(1) "7" } // string(2) "21" |
The code is as follows | Copy code |
unset($b); unset($c); var_dump($a); // Output: //array(2) { [0]=> string(2) "21" [1]=> int(7) } |
Avoid using PHP references
This is actually something to pay attention to mentioned in the PHP Array Manual. It most often occurs in foreach, where you hope to change the value of a far array through a reference (see this article).
In fact, I want to change the value of the array element by using foreach with references, mainly because PHP's array is an Associative Array. This kind of array has "indefinite length, the index can be discontinuous, and strings and integers can be used as indexes at the same time." So we cannot simply increment the integer index using a for loop.
Of course we can directly change the value of the array element through $key like the code below, but this may have certain efficiency issues.
The code is as follows
|
Copy code
|
||||
foreach ($array_var as $key => $value) $array_var [$key] = $newValue;
| Another common place for references is to pass parameters by reference in function calls. The main reason is to use this method to allow the function to return multiple return values. For example, we want to use a representation to indicate whether an error occurs during function execution and the return value is invalid.
A better way to operate it is to use unset on the reference variable immediately when it is no longer needed to switch the connection with the content. And even if the variable is not a reference type, we confirm that it is no longer used, and there will be no problem calling unset on it. At least it is guaranteed that reassigning the variable later will not affect the previous result.
http: //www.bkjia.com/PHPjc/631276.htmlTechArticleUse PHP’s References with Care Reference types (Reference) are used in many computer languages, and are used as a very Powerful and useful features exist. It has a pointer-like implementation...