PHP's Quote

Source: Internet
Author: User
PHP references

??? The so-called PHP reference is a different name that accesses the same variable content. Can be used on variables, functions, and objects, with the use of a & symbol in front of them. The following are the types and effects of references:

?

One, reference type

1.1. Variable references: Two variables pointing to the same content

 
  


1.2. function Address Reference

?

Note that the test (1) is here; Then it's going to go wrong.

1.3. Function Reference

?

The explanations are as follows:
???? $a =test () call the function, just assign the value of the function to $ A, and $ A does nothing to affect the $b in the function, and call the function by $a=&test (), his function is to return the memory address of the $b variable in the $b with the $ The memory address of a variable points to the same place, which produces the equivalent effect ($a =&b;) So change the value of $ A and change the value of the $b at the same time, so after executing the


$a =&test ();
$a = 5;


Later, the value of the $b becomes 5

This is to facilitate the understanding of the function's reference return before using a static variable, in fact, the function of the reference return is more useful in the object

?

1.4. References to Objects

?

The above code is the operation effect in PHP5;
In PHP5, the replication of an object is done by reference. In the above example $b=new A; $c = $b; is actually equivalent to $b=new A; $c =& $b;
The default in PHP5 is to invoke the object by reference, but sometimes you might want to make a copy of the object, and you want the original object to change without affecting the copy. For this purpose, PHP defines a special method called __clone.

Second, the role of reference


If the program is larger, referencing the same object is more variable, and you want to use the object after the manual removal of it, the personal suggestion "&" method, and then $var=null to clear. At other times, it's the default way to use PHP5. In addition, in the PHP5 for large arrays of delivery, it is recommended to use "&" mode, after all, saving memory space to use.

Third, dereference


When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the contents of the variable are destroyed. For example:

?? $a = 1;
?? $b =& $a;
?? unset ($a);
?>

Will not unset $b, but destroy the $a.

As global references, a reference to a global variable is actually established when a variable is declared with the global $var. In other words, it is the same as doing this:

??? $var =& $GLOBALS ["var"];
?>

This means, for example, that the unset $var does not unset global variables.

$this reference: In the method of an object, $this is always a reference to the object that invokes it.

?

Ps:

One advantage of PHP is that it can take advantage of its own automatic garbage collection mechanism (freeing up memory). That is, you do not need to do any free memory processing after the use of variables, PHP will help you complete. Of course, we can call the unset () function to free up memory as we wish, but this is not usually necessary.


In PHP, however, there is at least one situation where memory is not automatically released, as follows:


If there is a mutually referenced relationship between two objects, such as "Parent-child object", calling Unset () on the parent object does not release memory that references the parent object in the child object (even if the parent object is garbage collected).


This time, even manually calling unset (). For details, please refer to http://bugs.php.net/bug.php?id=33595.

The tradeoff is to call the __destruct () method of the object in the unset () function;

?

?

------------------------------------------------

Add:


PHP in the direction of the address (similar to the pointer) function is not implemented by the user itself, is implemented by the Zend Core, PHP refers to the use of "copy-on-write" principle, that is, unless a write operation, the same address to the variable or object is not copied.

The popular Speaking
1: If you have the following code

$a = "ABC";
$b = $a;

In fact, $a and $b are all pointing to the same memory address, not $ A and $b taking up different memory

2: If you add the following code based on the above code

$a = "EFG";

Since $ A and $b point to the memory of the data to be re-written once, at this time the Zend core will automatically determine, automatically for $b production of a $ A copy of the data, re-request a piece of memory for storage.

  • 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.