PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP reference ??? PHP references allow different names to access the same variable content. It can be used on variables, functions, and objects. the usage is to add the & amp; symbol before them. The following describes the reference types and their functions :? I. reference type 1.1 and variable reference: the two variables point to the same content & lt ;? Php $ aabc; $ B & amp; $ a; echo $ a; output here: reference of abcec PHP

??? PHP references allow different names to access the same variable content. It can be used on variables, functions, and objects. the usage is to add the & symbol before them. The following describes the types and functions of references:

?

I. reference type

1.1. variable reference:Two variables direct to the same content

  


1.2 function address reference

Function test (& $ a) {$ a = $ a + 100;} $ B = 1; test ($ B ); // Here $ B is actually the memory address of the $ B variable content passed to the function. by changing the value of $ a in the function, you can change the value of $ B by echo $ B; // output 101
?

Note that test (1); will cause an error.

1.3 Function Reference

Function & test () {static $ B = 0; // declare a static variable $ B = $ B + 1; echo $ B; return $ B ;} $ a = test (); // $ B value: 1 $ a = 5; $ a = test (); // $ B value: 2 $ a = & test (); // $ B value: 3 $ a = 5; $ a = test (); // $ B Value: 6
?

Explanation:
???? $ A = test () is used to call a function. it only assigns the value of the function to $ a. any change made to $ a does not affect $ B in the function, but how to call a function through $ a = & test, the function is to direct the memory address of the $ B variable in return $ B to the same place as the memory address of the $ a variable, that is, the equivalent effect ($ a = & B;) is generated, so the value of $ a is changed, and the value of $ B is also changed.


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


Later, the value of $ B is changed to 5.

Static variables are used for the convenience of understanding the reference and return functions. In fact, the reference and return functions are mostly used in objects.

?

1.4. object reference

Class testa {var $ abc = "ABC";} $ B = new testa; $ c = & $ B; // or $ c = $ B; echo $ B-> abc; // Here ABC echo $ c-> abc; // Here ABC $ B-> abc = "DEF" is output "; echo $ c-> abc; // output DEF here
?

The above code is the running effect in PHP5;
In PHP5, object replication is implemented through reference. In the preceding example, $ B = new a; $ c = $ B; is equivalent to $ B = new a; $ c = & $ B;
In PHP5, the object is called by reference by default, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. For this purpose, PHP defines a special method called _ clone.

II. Role of reference


If the program is large, there are many variables that reference the same object, and you want to manually clear the object after it is used up, I suggest using the & method, then, clear it in the form of $ var = null. In other cases, use the default php5 method. In addition, we recommend that you use the "&" method for transferring large arrays in php5 to save memory space.

III. cancel reference


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

?? $ A = 1;
?? $ B = & $;
?? Unset ($ );
?>

No unset $ B, but $ a is destroyed.

Another example is global reference. when a variable is declared using global $ var, a reference to the global variable is actually created. That is to say, it is the same as doing so:

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

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

$ This reference: in an object method, $ this is always a reference to the object that calls it.

?

PS:

One advantage of PHP is that it can use its automatic garbage collection mechanism (releasing memory ). That is, you do not need to release the memory after using the variable. PHP will help you. Of course, we can call the unset () function as needed to release the memory, but this is usually not required.


However, in PHP, the memory will not be automatically released in at least one case, as shown below:


If there is a mutual reference relationship between the two objects, such as "parent object-sub-object", call unset () for the parent object () the memory of the parent object referenced in the sub-object is not released (even if the parent object is reclaimed by garbage collection ).


In this case, you can call unset () manually (). Details available: http://bugs.php.net/bug.php? Id = 33595.

The alternative is to call the _ destruct () method in the object in the unset () function;

?

?

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

Supplement:


In php, the address pointing (similar to pointer) function is not implemented by the user, but is implemented by the Zend core. in php, the reference uses the principle of "copy at Write, unless a write operation occurs, the variables or objects pointing to the same address will not be copied.

In layman's terms
1: if the following code exists:

$ A = "ABC ";
$ B = $;

In fact, both $ a and $ B point to the same memory address, but not $ a and $ B occupy different memory.

2: Add the following code on the basis of the above code:

$ A = "EFG ";

Because the memory data pointed to by $ a and $ B needs to be re-written, the Zend core will automatically determine and automatically generate a $ a data copy for $ B, apply for a new 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.