Home >Backend Development >PHP Tutorial >PHP reference counting and variable reference_PHP tutorial

PHP reference counting and variable reference_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:08:37883browse

php reference counting and variable reference

Each php5.5 variable is stored in a variable container called zval.
A zval variable container, in addition to the type and value of the variable, also contains two bytes of additional information:
 1. The first one is "is_ref", which is a bool type and is used to identify whether this variable belongs to the reference set. If it does, its value is 1, otherwise it is 0.
With this variable, the php engine can distinguish ordinary variables from reference variables.
 2. The second one is "refcount", which is used to indicate the number of points pointing to this zval variable (symbol). Every symbol has a scope, and so do the main scripts and functions or methods.
All symbols exist in a symbol table.
When a variable is assigned a constant value, a zval variable container will be generated, as in the following example:
cd790cf977c5769ff49ec77aa7401589
At this time, execute the following program to get the $a variable pointing to the is_ref and refcount values ​​​​in the zval container
d891614e2dcbdbca7621b70217076a49
a: (refcount=1, is_ref=0)='Hello world'
Next, we conduct the following experiments to discuss reference assignment and ordinary assignment.
First, make $b point to $a, check is_ref, refcount, as follows:
2448aa0b5c5bb33362d04ebae708ac41
a: (refcount=2, is_ref=0)='Hello world'
b: (refcount=2, is_ref=0)='Hello world'
Let $b refer to $a, check is_ref refcount, as follows
e97ff46a6ae781d1ff1eb55fd06924c8
a: (refcount=2, is_ref=1)='Hello world'
b: (refcount=2, is_ref=1)='Hello world'
From the above we can analyze that when a variable refers to the corresponding zval container, is_ref is 1.
We analyze further, we refer $b to $a, $c points to $a, as follows
<?php
    $a = "Hello world";  
    $b = &$a;
    $c = $a;

    print_r(xdebug_debug_zval(&#39;a&#39;));
    print_r(xdebug_debug_zval(&#39;b&#39;));
    print_r(xdebug_debug_zval(&#39;c&#39;));
?>

The print results are as follows
a: (refcount=2, is_ref=1)='Hello world'
b: (refcount=2, is_ref=1)='Hello world'
c: (refcount=1, is_ref=0)='Hello world'
It can be seen that at this time, the php5.5 engine re-establishes a zval container for $c. The data type and value in the container are exactly the same as those in the container pointed to by $a. The difference is the value of its refcount and is_ref.
Therefore, we can see that the is_ref variable in the zval container of php5.5 either identifies a reference collection or a normal collection. When both are present, it will clone the zval container to resolve the conflict problem.
 
Summary:
1. After php5.5, "variable assignment" refers to point assignment, that is, a variable points to a specific zval container.
 2. "Variable reference" binds variables to variables. If one of the bound variables changes its direction, the directions of other variables bound to each other will also change.
If a variable re-references a variable, its original variable binding is released and the new variable is bound instead. The following code:
 
<?php
function foo(&$var)
{
    $var =& $GLOBALS["baz"];
}
foo($bar);
?>

This will cause the $var variable in the foo function to be bound to $bar when the function is called, but then re-bound to $GLOBALS["baz"]. It is not possible to bind $bar to another variable within the function call scope through the reference mechanism, because there is no variable $bar in function foo (it is represented as $var, but $var only has the variable content and no call symbol table name-to-value binding). You can use reference returns to reference variables selected by the function.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1053350.htmlTechArticlephp reference counting and variable reference Each php5.5 variable is stored in a variable container called zval. A zval variable container, in addition to the type and value of the variable, also contains two bytes...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn