PHP variable declaration and assignment, the variable name exists in the symbol table, and the value and class information exists in Zval, Zval contains four variables,is_ref,refcount,value,type,zval source code is as follows
Copy Code code 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 represents the number of the value address and the same zval, Refcount=0, Zval is destroyed
Is_ref Indicates whether a zval is referenced and has "0" and "1" states
Here's an analysis of when zval will be copied or opened up new memory space.
1. When is_ref=0, and refcount>1, if you change the value of a variable that points to the Zval, a new zval is generated, zval of the original refcount--, such as: $a =1; $b = $a; $b =2;,zval will be replicated, In other words, AB points to the same zval, and later B uses the newly opened Zval
2. When is_ref=0, and refcount>1, if Zval is assigned to a reference variable, the variable used to assign the value and the variable and the assigned value will use the same original zval, while the other variables pointing to the original Zval will point to a new replicated zval. And RefCount will be recalculated, for example: $a =1 $b = $a, $c = $a, $d =& $a, at which time AD uses the original ZVAL,BC to use the newly copied zval
3. When is_ref=1, and refcount>1, if Zval is copied to a unreferenced variable, the unreferenced variable uses a newly replicated zval, and the zval of the meta RefCount is unchanged, for example: $a =1; $b =& $a; $c = $a, So AB uses the original zval, and C uses the new replicated Zval
Type represents the value type of the Zval, and the macro defines the following
Copy Code code 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 Zval, and he is a community with the following code
Copy Code code as follows:
typedef Union _ZVALUE_VALUE {
Long lval; /* Long Value * *
Double Dval; /* Double Value * *
struct {
Char *val;
int Len;
} str;
HashTable *ht; /* Hash Table value * *
Zend_object_value obj;
} Zvalue_value;
Now you know how PHP is transformed, because his value is actually a struct that can represent any type of structure, and a specific value depends on the type to determine which variable in the community is to be stored.
See Example 1 below
Copy Code code as follows:
.-----------
$a = 1;
$b = $a;
$c = $a;
.-----------
$d = & $a;
.-----------
$a = 2;
.-----------
$b = null;
view changes in Refcount,is_ref,zval
Finish the first part and then look at the output
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
you can see that a,b,c use the same zval.
And look at the second part of the 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 that at this time a,d together, they use the same zval, while BC uses a newly generated zval and recalculates two zval refcount and Is_ref
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 tell that the value of a good base for two is_ref=1 of ad is 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 because of their zval is_ref=0, so they are not good base friends, their values will not change at the same time, so the BC Zval split again, b = null c = 1