From an interview, we will talk about the common pass-through, reference transfer, and unset of PHP.

Source: Internet
Author: User
This concept is generally introduced when talking about variables in the first PHP lesson, and I used to introduce it to other PHPer. But as a PHPer who worked for a period of time, I suddenly lost my face when I got a wrong answer during the interview.

First, you need to understand that the variable name is stored in the memory stack. it is the address pointing to the specific memory in the heap. you can use the variable name to find the memory in the heap;
A common value is a different address name that points to different memory entities;
Reference to pass a value. after passing a reference, yesDifferent address names point to the same memory entity. if one address is changed, the other address is changed;

Here I will explain the differences between the two values in detail through three columns:

Example1:

 

Example2:

 $ Param1 = '. $ param1 .'
'; // Display as $ param1 = 1 echo'
$ Param2 = '. $ param2 .' '; // It is displayed as $ param2 = because $ param2 is a local variable, it is automatically destroyed after the function is run. it cannot affect the global effect, php is not recommended, and php. if this parameter is set in, an error is returned. $ param1 = 1; function add ($ param2) {$ param2 = 3; return $ param2 ;}$ param3 = add (& $ param1 ); // call the add method and pass the reference of variable 1 to variable 2. at this time, the two addresses direct to the same memory and change one address. the other address must also be changed. echo $ param1; // 3, the memory has changed inside the function; echo $ param3; // 3?>

Example3:

 $ V) {$ v + = 10; // 1. invalid change. it is equivalent to throwing the retrieved key value to the variable $ v, and then changing the value of the variable $ v, irrelevant to the array; echo $ v. ""; // output 13 15;} foreach ($ arr as $ k => $ v) {$ arr [$ k] + = 10; // 2. valid change: directly change the value in the key name; echo $ v; // output 3, 5;} foreach ($ arr as & $ v) {$ v + = 10; // 3. valid change. The traversal key value is directly assigned to the $ v address, which is actually the key name .. $ v + 10 equals to $ arr [$ k] + 10;}?>

Then let's take a look at this question:

$a = 1; $b = &$a; unset($a); echo $b; //??
At that time, my answer was as follows: a notice error will be reported, because $ B and $ a Share a memory, unset will destroy the memory, the memory is not found when the $ B variable is output, so it should be Notice: Undefined variable: B in

However, note that unset does not actually destroy variables... it just breaks off the relationship between variables and memory, and the memory will not be released as long as it is still referenced;\ (B and \) a point to 1 at the same time, and cut off the \ (a relationship, \) B or point to 1. therefore, no error is reported in the above question, so 1 is output.

In addition, in PHP, the object's passing value is a reference passing value by default.

Next question:
Before doing this, let's review what destructor are and what methods are used to destroy objects in PHP:

Destructor: Executed when the object is destroyed. Note that in implicit destruction, the object is destroyed only when all php code executes the last line of code. Also, pay attention to the MVC mode under the single-entry file.

Object destruction:

  1. Explicit destruction: the object will be destroyed if it is not referenced. Therefore, we can unset or assign NULL values to it;
  2. Hidden test destruction: PHP is a scripting language. when the last line of code is executed, all applied memory will be released.

Example1:

Class Human {public $ name = 'Zhang San'; public $ gender = null; public function _ destruct () {echo 'is dead!
';}}$ A = new Human (); $ B = $ c = $ d = $ a; unset ($ a); echo' '; // Does the destructor trigger several times, online or offline?

Answer:

$ B = $ c = $ d = $ a; by default, the value is referenced. four variables point to the same memory. When unset, the object is still used by three other variables, so the object is not destroyed, so the destructor is triggered online (after the code is executed, the memory is automatically released)

Example2:

Class Human {public $ name = 'Zhang San'; public $ gender = null; public function _ destruct () {echo 'is dead!
';}}$ E = $ f = $ g = new Human (); unset ($ e); unset ($ f); unset ($ g); echo' '; // The same question: Is the destructor triggered online or offline?

I believe that through Example1, we should soon know that the answer is triggered online; before the code runs, the memory is automatically released because the object has not been referenced by any variable ....

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.