Reference types (Reference) are used in many computer languages and are present as a very powerful and useful feature. It has a similar pointer (pointer) implementation, but it has a different performance than the pointer. For example, a reference to C + + allows different variables to point to the same object, while maintaining the direct use of the dot to get the object members without cumbersome use of the dereference operator (*) and the pointer to-member operator (->). In Java and C #, direct references are the main types, as much as possible for developers to avoid using pointers.
The reference type is also introduced in PHP, which is basically considered to be the same as java/c# in the assignment of objects (see objects and references, for specific reference). However, it also supports the reference operator (&) on the underlying type to obtain the content. However, in actual use, the PHP reference type because of the entire PHP design structure and there are many problems, so that the program has unpredictable results.
reference variable can be given a new reference
In C + +, a variable of a reference type can only be given a reference value when it is defined, so as long as we trace it to the definition of the variable, we know what the variable is manipulating.
But PHP is different, PHP is fuzzy the definition of variables, you can not define the variables used. So you can have a variable given a reference value multiple times.
Copy Code code as follows:
$x = 21;
$y = 7;
$z = & $x;
$z = & $y;
Var_dump ($x, $y, $z);
For the first time, it makes sense that $z becomes a reference to $x, and then lets $z's content become a reference to $y, which means $x and $z are references to $y. But the actual output results are:
Copy Code code as follows:
As a result, the $x remained unchanged, but $z was changed into a reference to $y. Equivalent to unset the $z variable and then giving the new value.
Copy Code code as follows:
$z = & $x;
Unset ($Z);
$z = & $y;
This is actually a reasonable logic, like the code below, and we don't get a reference reference (Reference refer to a referenece) like "pointers to pointers (pointer point to a pointer)". Just multiple reference variables that refer to the same piece of content.
Copy Code code as follows:
$x = 21;
$y = & $x;
$z = & $y
referencing an array element makes that element a reference type
A reference to a variable does not change the type of the original variable, but if the element in the array is taken, it becomes 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 a array by reference.
This means that the array assignment of PHP is copy rather than reference, and the assignment process creates a new array that assigns the variable to the value. The array operation on the new variable does not affect the contents of the original array variable.
Copy Code code as follows:
$a = Array (21, 7);
$b = $a;
$b [0] = 7;
Var_dump ($a);
Echo ' <br/> ';
Var_dump ($b);
Output:
Array (2) {[0]=> int [1]=> int (7)}
Array (2) {[0]=> int (7) [1]=> int (7)}
Let's take a look at the bottom. If you refer to an element in an array, what is the exception.
Copy Code code as follows:
$a = Array (21, 7);
$c = & $a [0];
$b = $a;
$b [0]= "21";
$b [1]= "7";
Var_dump ($a);
Echo ' <br/> ';
Var_dump ($b);
Echo ' <br/> ';
Var_dump ($c);
Echo ' <br/> ';
Output:
Array (2) {[0]=> &string (2) "[1]=>" Int (7)}
Array (2) {[0]=> &string (2) "[1]=>" string (1) "7"}
String (2) "21"
The $b in the code is just a simple assignment, just a previous reference to the first element, but a new array should be copied. But the result is a modification of the $b, which also changes the first element of the $a, and the second element has no effect.
From the output we also see an unusual place, that is, the type of the first element of the array is more than one ' & ' symbol. And this is the reference operator. This means that the first element of the array has become a reference type. So the assignment is also a reference copy, not a value copy.
This problem is very strange, in the development also caused a lot of unnecessary trouble, originally thought that the copied array is not associated with the original array, but because of this unexpected reference type, let me in the operation also affected the original array.
I'm not sure if this is a bug in PHP, or is it intended to be so designed. On the internet for a long time did not have a relevant explanation for this convenience, only the float middle "php:references to Array Elements Are risky" and symmetric designs, "Problems w/ Accessing a PHP array by reference says this, but there's no reason for it.
And then in the PHP bug report, see a few associated reports (Bug6417, Bug7412, Bug15025, Bug20993). Some say this is a bug and have been repaired in the later version. Specifically I do not understand, you can only avoid using the reference on the array.
The more interesting thing is that if you unset those references and leave only one, then the array elements will become normal types that do not contain references.
Copy Code code as follows:
Unset ($b);
Unset ($c);
Var_dump ($a);
Output:
Array (2) {[0]=> string (2) "[1]=>" Int (7)}
Avoid using PHP references
This is actually something to note in the PHP Array manual, most often in foreach, where you want to change the value of a far array by reference (see the article).
In fact, you want to change the value of an array element by using a foreach reference, mainly because the PHP array is associative array, the array "indefinite length, the index can be discontinuous and indexed by both strings and integers," so we can't simply add an integer index with the for loop.
Of course, we can change the value of the array element directly by $key as the code below, but there may be some efficiency problems.
Copy Code code as follows:
foreach ($array _var as $key => $value)
$array _var [$key] = $newValue;
Another common reference is to pass parameters in a function call using a reference. The main reason for this is that you want the function implementation to return multiple return values in this way. For example, we would like to use a representation that indicates whether the function has an error in execution and that the return value is invalid.
However, because PHP functions can return different types, there is no need to pass in the reference argument as a representation. Even if you really need multiple return values, you can also return to the "array as a string" as a solution, but you may want to indicate in the document that each element corresponds to that result.
A better way to do this is to use unset on the variable whenever the reference variable is no longer needed , and let it switch the connection to the content. And even if the variable is not a reference type, we confirm that it is no longer in use and it is not a problem to call unset. At a minimum, ensuring that the variable is later assigned does not affect the previous result.
- Problems w/accessing a PHP array by reference-symmetric Designs
- php:references to Array Elements Are risky–float Middle
- References and Foreach-johannes Schlüter
- References explained-php Manual