1.1.1 zval structure
Zend uses the zval structure to store the value of PHP variables. The structure is as follows:
struct _zval_struct {
/* Variable information */
zvalue_value value; /* value */
zend_uint refcount;
zend_uchar type; /* active type */
zend_uchar is_ref;
};
typedef struct _zval_struct zval;
IS_NULL |
N/A |
IS_LONG |
对应value.lval |
IS_DOUBLE |
对应value.dval |
IS_STRING |
对应value.str |
IS_ARRAY |
对应value.ht |
IS_OBJECT |
对应value.obj |
IS_BOOL |
对应value.lval. |
IS_RESOURCE |
对应value.lval |
According to this table, two interesting things can be found: first, the array in PHP is actually a HashTable, which explains why PHP can support associative arrays; secondly, Resource is a long value, which usually stores a A pointer, the index of an internal array, or something else known only to the creator can be considered a handle.
1.1.2 Reference counting
Reference counting is widely used in garbage collection, memory pools, strings, etc. Zend implements typical reference counting. Multiple PHP variables can share the same zval through the reference counting mechanism. The remaining two members of zval, is_ref and refcount, are used to support this sharing.
Obviously, refcount is used for counting. When the reference is increased or decreased, this value is also incremented and decremented accordingly. Once it decreases to zero, Zend will recycle the zval.
What about is_ref?
1.1.3 zval status
In PHP, there are two types of variables - reference and non-reference, and they are all stored in Zend using reference counting. For non-reference variables, it is required that the variables are independent of each other. When modifying one variable, it cannot affect other variables. This conflict can be solved by using the Copy-On-Write mechanism - when trying to write a variable, Zend will find If the zval pointed to by this variable is shared by multiple variables, a zval with a refcount of 1 will be copied to it, and the refcount of the original zval will be decremented. This process is called "zval separation". However, for reference variables, the requirements are opposite to those for non-reference types. Variables assigned by reference must be bundled. Modifying one variable modifies all bundled variables.
It can be seen that it is necessary to point out the status of the current zval to deal with these two situations respectively. is_ref is for this purpose. It points out whether all the variables currently pointing to the zval are assigned by reference - either all references or Not at all. At this time, another variable is modified. Only when it is found that the is_ref of its zval is 0, that is, it is not a reference, Zend will execute Copy-On-Write.
1.1.4 zval status switching
When all assignment operations on a zval are references or non-references, one is_ref is enough to cope with it. However, the world is not always so beautiful. PHP cannot impose such restrictions on users. When we mix reference and non-reference assignments, special handling must be carried out.
Situation I, look at the following PHP code:
For case II, look at the following PHP code:
1.1.5 Parameter passing
The passing of PHP function parameters is the same as variable assignment. Non-reference passing is equivalent to non-reference assignment, and reference passing is equivalent to reference assignment, and may also cause Perform zval state switching. This will be mentioned later.