Home >Backend Development >PHP Tutorial >In-depth analysis of PHP's reference counting mechanism_PHP tutorial

In-depth analysis of PHP's reference counting mechanism_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:06:36862browse

After PHP variables are declared and assigned, the variable name is stored in the symbol table, and the value and class information are stored in zval. Zval contains four variables, is_ref, refcount, value, type, zvalThe source code is as follows

Copy codeThe code is as follows:

struct _zval_struct {
/* Variable information * / Zvalue_value value; / * value * /
Zend_uint Refcount__gc;
Zend_uchar Type; / * Active Type * /
Zend_uchar is_Ref__GC; };

refcount indicates how many zvals have the same value address. When refcount=0, the zval is destroyed
is_ref indicates whether a zval is referenced. There are two states: "0" and "1"

Here is an analysis of when zval will be copied or new memory space opened up
1. When is_ref=0, and refcount>1, if it changes The value of a variable pointing to the zval will generate a new zval, the refcount of the original zval--, for example: $a=1;$b=$a;$b=2;, the zval will be copied, that is to say, the original zval ab points to the same zval, and later b will use the newly opened zval

2. When is_ref=0, and refcount>1, if zval is assigned to a reference variable, then it is used to assign the value and the variable sum The assigned variable will use the same original zval, and other variables pointing to the original zval will point to a newly copied zval, and the refcount will be recalculated, for example: $a=1;$b=$a;$c= $a;$d=&$a;, at this time ad uses the original zval, and bc uses the newly copied zval

3. When is_ref=1 and refcount>1, if zval is copied to someone A non-reference variable, the non-reference variable will use a newly copied zval, and the refcount of the original zval remains unchanged. For example: $a=1;$b=&$a;$c=$a, then ab uses the original zval, And c uses the newly copied zval
type to represent the value type of the zval. The macro definition is as follows:

Copy code The code is as follows:
#define IS_NULL 0
#define IS_LONG 1
#define IS_DOUBLE 2
#define IS_BOOL 3
#define IS_ARRAY 4
#define IS_OBJECT 5 >#define IS_STRING 6
#define IS_RESOURCE 7
#define IS_CONSTANT 8
#define IS_CONSTANT_ARRAY 9


value represents the value of the zval, which is also a community. The code is as follows


Copy code The code is as follows:
typedef union _zvalue_value { /* double value */
struct {
char *val;
int len;
} str;
HashTable *ht; /* hash table value */
zend_object_value ob j;
} zvalue_value;


Now you know how PHP converts types, because its value is actually stored in a structure that can represent any type, and the specific value is determined based on the type. Which variable in the community is used to store the value?

See example 1 below



Copy the code
The code is as follows: .----------$a = 1;
$b = $a;
$c = $a;
.-----------
$d = &$a;
.-----------
$a = 2;
.- ----------
$b = null;


Check the changes in refcount, is_ref, zval
Look at the output after executing the first part
1------------------ -----------
a:(refcount=3, is_ref=0),int 1
b:(refcount=3, is_ref=0),int 1
c: (refcount=3, is_ref=0), int 1
It can be seen that a, b, c use the same zval
and then look at the second part of execution
2-- --------------------------
a:(refcount=2, is_ref=1),int 1
b:(refcount =2, is_ref=0),int 1
c:(refcount=2, is_ref=0),int 1
d:(refcount=2, is_ref=1),int 1
Note at this time a, d are together, they use the same zval, and bc uses a newly generated zval, and recalculates the refcount and is_ref of the two zvals at the same time
3-------------- ---------------
a:(refcount=2, is_ref=1),int 2
b:(refcount=2, is_ref=0),int 1
c:(refcount=2, is_ref=0),int 1
d:(refcount=2, is_ref=1),int 2
You can know the values ​​of ad these two good friends with is_ref=1 are changed at the same time
4----------------------------------
a: (refcount=2, is_ref=1) ,int 2
b:(refcount=1, is_ref=0),null
c:(refcount=1, is_ref=0),int 1
d:(refcount=2, is_ref=1) , int 2
bc Since their zval’s is_ref=0, they are not good friends, and their values ​​will not change at the same time, so bc’s zval splits again, b = null c = 1

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327623.htmlTechArticleAfter PHP variables are declared and assigned values, the variable name is stored in the symbol table, and the value and class information are stored in zval. zval contains four variables, is_ref, refcount, value, type, zval source code is copied as follows...
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