Careful use of PHP citation reason analysis _php tutorial

Source: Internet
Author: User
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 is different from the performance of the pointer. For example, C + + reference, you can have different variables to point to the same object, while maintaining the direct use of the dot to get object members, without tedious use of the dereference operator (*) and the pointer to member operator (.). In Java and C #, direct reference is the primary type, so that developers can avoid using pointers as much as possible.

The reference type is also introduced in PHP, which is basically considered to be a reference pass with the java/c# (see objects and references for details) on the assignment of the object. However, it also supports the reference operator (&) on the underlying type to obtain a reference to 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.


A reference variable can be assigned a new reference

In C + +, a variable of a reference type can only be given a reference value when it is defined, so we can just trace it to the definition of the variable to know which content the variable is manipulating.

However, PHP differs in that it blurs the definition of a variable and can be used without defining it. So you can let a variable be given a reference value multiple times.
Copy CodeThe code is as follows:
$x = 21;
$y = 7;

$z = & $x;
$z = & $y;

Var_dump ($x, $y, $z);

For the first time, the feeling is that $z became a reference to $x, and then let $z's content become a reference to $y, which means $x and $z are references to $y. But the actual output is:
Copy CodeThe code is as follows:
Int (21)
Int (7)
Int (7)

As seen from the results, the $x remained the same, but $z was changed into a reference to $y. It is equivalent to unset the $z variable before assigning a new value.
Copy CodeThe code is as follows:
$z = & $x;
Unset ($Z);
$z = & $y;

This is actually reasonable logic, like the code below, and we're not getting a reference reference (Reference refer to a referenece) like "Pointer to pointer (Pointer point to a Pointer)", Just multiple reference variables referencing the same piece of content.
Copy CodeThe code is as follows:
$x = 21;
$y = & $x;
$z = & $y


referencing an array element causes the element to become a reference type

A reference to a variable does not cause a change in the type of the original variable, but if the element in the array is taken, the element becomes a reference type.

Before looking at the problem code, the first thing to say is:
Array assignment always involves value copying. Use the reference operator-to-copy an array by reference.

That is, the assignment of an array of PHP is copy, not a reference, and the assignment process creates a new array to assign the assigned variable. An array operation on a new variable does not affect the contents of the original array variable.
Copy CodeThe code is as follows:
$a = Array (21, 7);
$b = $a;
$b [0] = 7;
Var_dump ($a);
Echo '
';
Var_dump ($b);

Output:
Array (2) {[0]=> int (+) [1]=> int (7)}
Array (2) {[0]=> int (7) [1]=> int (7)}

Let's see what happens if you reference an element in an array.
Copy CodeThe code is as follows:
$a = Array (21, 7);
$c = & $a [0];
$b = $a;
$b [0]= "21";
$b [1]= "7";

Var_dump ($a);
Echo '
';
Var_dump ($b);
Echo '
';
Var_dump ($c);
Echo '
';

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, but a reference to the first element has been added before, but a new array should have been copied. But the result is a modification to the $b, which also changes the first element of $ A, while the second element has no effect.

From the output we also see an unusual place, is the array of the first element of the type more than a ' & ' 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 the unexpected type of reference, let me in the operation also affected the original array.

I don't know if this is a bug in PHP, or if it's intentionally designed. On the internet for a long time there is no relevant explanation for the convenience, only the float middle "php:references to Array Elements is risky" and symmetric designs "problems w/ Accessing a PHP array by reference has talked about this, but there is no reason.

Then in PHP bug report see a few linked reports (Bug6417, Bug7412, Bug15025, Bug20993). Some say this is a bug and have been repaired in the later version. Specifically, I don't understand that you can only avoid using references on arrays.

The more interesting thing is that if you unset those references and leave only one, then the array elements become normal types that do not contain references.

Copy the Code code as follows:
Unset ($b);
Unset ($c);
Var_dump ($a);

Output:
Array (2) {[0]=> string (2) "[1]=>" Int (7)}


Avoid using PHP's references

This is actually the place to note in PHP array manual, most often in foreach, in the hope of changing the value of the far array by reference (see this article).

In fact, you want to change the value of an array element by using a foreach mate reference, mainly because the array of PHP is a associative array, which is "indefinite length, the index can be discontinuous, and can be indexed with both strings and integers," so we cannot simply increase the integer index with a 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 the Code code as follows:
foreach ($array _var as $key = $value)
$array _var [$key] = $newValue;

Another common reference is the use of reference passing parameters in function calls. The main reason for this is to let the function implementation return multiple return values in this way. For example, we want to use a representation that indicates whether the function has an error in execution and causes the return value to be invalid.

However, because PHP functions can return different types, it is not necessary to pass in a reference parameter as a representation. Even if you really need more than one return value, you can do this by returning an array with the string as the primary key, but you may need to indicate in your document that each element corresponds to that result.

There is a good way to do this, whenever the reference variable no longer needs to be used, the variable is immediately used unset let it switch to the relationship between the content. And even if the variable is not a reference type, we confirm that it is no longer in use and that it will not be a problem to call unset. At a minimum, ensure that the variable is subsequently re-assigned, without affecting the previous result.

    1. Problems w/accessing a PHP array by Reference-symmetric Designs
    2. Php:references to Array Elements is risky–float middle
    3. References and Foreach-johannes Schlüter
    4. References explained-php Manual

http://www.bkjia.com/PHPjc/325843.html www.bkjia.com true http://www.bkjia.com/PHPjc/325843.html techarticle 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 is different from ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.