PHP Reference Counter Popular version explanation _php tutorial

Source: Internet
Author: User

PHP Reference Counter Popular version explanation


Overview

Recently See the Reference Counter section of PHP, first by a variety of Halo, and then by looking at the blog and analysis, summed up a more popular explanation, can help oneself very good memory, but also hope to help you readers. Here is a more orthodox explanation: a reference to PHP variables (http://hilojack.sinaapp.com/?p=1392).

Suggestions

The study of PHP reference counter changes can be learned by installing the xdebug extension, which calls Xdebug_debug_zval (' var ') directly after installation to see the reference counter of the variable $var.

Basic knowledge

The reference counter needs to understand the mechanism of storing and referencing variables in PHP, and the reference counting is of course to save memory, and let multiple variable symbols share a memory value space (also known as a variable container) without affecting semantic correctness. When the reference count is changed: assignment . There are two types of assignment: value passing assignment and reference assignment. More difficult to understand is the reference assignment. Another function of the reference count is to indicate when the same space can be shared and when the variable must be separated (another space is opened).

Popular explanations

In order to facilitate the understanding and memory, I give the various scenarios in the assignment to the popular interpretation, not with the actual situation,.

The & is a combination that can be equivalent to marriage, but it is more perverted to allow more than one person to marry in PHP (polygamy or a polygamous system). Note that marriage must live together, separation is not possible. In addition to marriage there is a pattern called co-rental. Co-renting is a cohabitation, but there is no relationship , allowing more people to co-rent, this is reasonable. There is, of course, a situation where living alone is easier to understand. The following example shows an assignment statement that corresponds to these three states:

Scenario One:

$a = "a";  $a solitary, Is_ref = 0, refcount = 1; $b = $a;   $a and $b co-rental, Is_ref = 0, refcount = 2;
Scenario Two:

$a = "a"; $a solitary, Is_ref = 0, refcount = 1; $b = & $a; $a marry $b, is_ref = 1, refcount = 2;
As can be seen above, is_ref can be understood as a marriage certificate, =1 said two and a number of variables are marriage relationship, =0 not married (can be rented or cohabitation), RefCount said how many variables live together, =1 said solitary living, >1 said that many people cohabitation (remember, marriage must cohabitation, But cohabitation is not necessarily a marriage relationship.

Here's how to start analyzing variable relationships when assigning values:

Scenario One:

$a = "a";   $a solitary, Is_ref = 0, refcount = 1; $b = $a; $a and $b co-rental, Is_ref = 0, refcount = 2; $va = "B"; $va solitary, Is_ref = 0, refcount = 1; $vb = $va;  $va and $vb co-rental, Is_ref = 0, refcount = 2; $a = $va; $a is single, $va is single, so $a moved to live with $va, now $ A, $va, $vb three people living together after assignment: $a: is_ref = 0, RefCount = 3, String = "xyz" $b: is_ref = 0, Refcoun t = 1, String = "Qwe" $va: is_ref = 0, RefCount = 3, string "xyz" $VB: is_ref = 0, RefCount = 3, string "XYZ"

Scenario Two:

$a = "a";   $a solitary, Is_ref = 0, refcount = 1; $b = & $a; $a marry $b, is_ref = 1, refcount = 2; $va = "B"; $va solitary, Is_ref = 0, refcount = 1; $vb = $va;  $va and $vb co-rental, Is_ref = 0, refcount = 2; $a = $va; $a is married, $a can't move out alone, the assignment will copy the value of $va to $ A, other relationships will not change $a:is_ref = 1, RefCount = 2, String = "xyz" $b: Is_ref = 1, refcount = 2, String = "xyz" $va: is_ref = 0, RefCount = 2, string "xyz" $VB: is_ref = 0, RefCount = 2, string "XYZ"

Scenario Three:

$a = "a";   $a solitary, Is_ref = 0, refcount = 1; $b = & $a; $a marry $b, is_ref = 1, refcount = 2; $va = "B"; $va solitary, Is_ref = 0, refcount = 1; $vb = & $va;  $va and $vb Marry, Is_ref = 1, refcount = 2; $a = $va; $a is married, same as scenario two, value copy, relationship invariant after assignment: $a: is_ref = 1, RefCount = 2, String = "xyz" $b: Is_ref = 1, RefCount = 2, String = "xyz" $va: Is_ref = 1, RefCount = 2, string "xyz" $VB: Is_ref = 1, RefCount = 2, string "XYZ"

Scenario Four:

$a = "qwe";  $a solitary, Is_ref = 0, refcount = 1; $b = $a;   $a cohabitation with $b, Is_ref = 0, refcount = 2; $va = "xyz"; $va solitary, Is_ref = 0, refcount = 1; $vb = & $va; $va and $vb Marry, Is_ref = 1, refcount = 2; $a = $va;  $a want to live with $va (rather than marry), but $va married, so $a can only be moved out of the $b, redistributed a house, the same value as $va (the term "variable separation"); $va and $vb relationship invariant: $a: is_ref = 0, RefCount = 1, stri  ng = "xyz" $b: is_ref = 0, RefCount = 1, String = "Qwe" $va: is_ref = 1, RefCount = 2, string "xyz" $VB: Is_ref = 1, refcount = 2, string "XYZ"

Scenario Five:

$a = "qwe";  $a solitary, Is_ref = 0, refcount = 1; $b = $a;   $a cohabitation with $b, Is_ref = 0, refcount = 2; $va = "xyz"; $va solitary, Is_ref = 0, refcount = 1; $vb = $va; $va and $vb cohabitation, Is_ref = 0, refcount = 2; $a = & $va;  $a want to marry $va, now $ A and $va are single but have roommates, so they each move out of the original place, and then separate a new house, the value and $va the same value after: $a: is_ref = 1, RefCount = 2, String = "xyz" $ B:is_ref = 0, RefCount = 1, String = "Qwe" $va: is_ref = 1, RefCount = 2, string "xyz" $VB: is_ref = 0, RefCount = 1, Strin G "XYZ"

Scenario Six:

$a = "qwe";  $a solitary, Is_ref = 0, refcount = 1; $b = $a;   $a cohabitation with $b, Is_ref = 0, refcount = 2; $va = "xyz"; $va solitary, Is_ref = 0, refcount = 1; $vb = & $va; $va and $vb Marry, Is_ref = 1, refcount = 2; $a = & $va;  $a wanted to marry $va, but $va was married and $a was single, so $a moved in and $va, $va now has two spouses: $VB and $a are assigned: $a: is_ref = 1, RefCount = 3, string = "xyz "$b: is_ref = 0, RefCount = 1, String =" Qwe "$va: is_ref = 1, RefCount = 3, string" xyz "$VB: Is_ref = 1, refcount = 3, str ing "xyz"

Scenario Seven:

$a = "qwe";  $a solitary, Is_ref = 0, refcount = 1; $b = & $a;   $a marry $b, is_ref = 1, refcount = 2; $va = "xyz"; $va solitary, Is_ref = 0, refcount = 1; $vb = $va; $va and $vb cohabitation, Is_ref = 0, refcount = 2; $a = & $va;  $a want to marry $va, but $a is married, $va is single, the solution is to $a married after divorce, and $va to move out of the same place as $va. After assignment: $vb: is_ref = 1, RefCount = 2, String =  "XYZ" $b: is_ref = 0, RefCount = 1, String = "Qwe" $va: is_ref = 1, RefCount = 2, string "xyz" $VB: is_ref = 0, RefCount = 1, String "xyz"


Scenario Eight:

$a = "qwe";  $a solitary, Is_ref = 0, refcount = 1; $b = & $a;   $a marry $b, is_ref = 1, refcount = 2; $va = "xyz"; $va solitary, Is_ref = 0, refcount = 1; $vb = & $va; $va and $vb Marry, Is_ref = 1, refcount = 2; $a = & $va;  $a want to marry $va, but $a and $va are married, who divorces? $a!, because is $ A actively want to marry $va,//$a after divorce $va live together, $va now have two spouses: $VB and $a assigned: $a: is_ref = 1, RefCount = 3, String = "xyz" $b: is_ref = 0, re Fcount = 1, String = "Qwe" $va: is_ref = 1, RefCount = 3, string "xyz" $VB: Is_ref = 1, RefCount = 3, string "XYZ"

The above analysis of the simple variable assignment of various cases, not including self-reference cases. In general terms:

Simple assignment is cohabitation, need to examine the is_ref of the left side of the equal sign (that is, whether married), if is_ref = 1, the value of copy, otherwise consider the left, whether in the case of no additional allocation of memory, and the right variable share the same storage space (cohabitation), at this time to examine whether the right is married, if, Can not live together, variable separation, if the right variable is also single, then directly share the same memory, all cohabiting people follow the principle of COW (copy-on-write).

Reference assignment is a combination, you need to examine the right variable reference, if Is_ref = 1, then direct RefCount + +, there is a case of multi-person combination, if the right is a non-reference (is_ref = 0), it is also necessary to investigate whether the right is solitary, if alone, then left and right of the two variables to share a variable space , otherwise the right variable separates from the original place and opens up a new space on the left. Whenever a reference assignment is made, the left variable is always separated from the previously combined or shared variable.







http://www.bkjia.com/PHPjc/875473.html www.bkjia.com true http://www.bkjia.com/PHPjc/875473.html techarticle PHP Reference Counter Popular version of the explanation of the recent reading in PHP Reference counter section, first by a variety of Halo, and then by looking at the blog and analysis, summed up a more popular explanation, ...

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