In-depth explanation of PHP variable Storage

Source: Internet
Author: User

1.1.1 zval Structure
Zend uses the zval structure to store the values of PHP variables. The structure is as follows:

Copy codeThe Code is 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;

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;

Zend determines which member to access the value based on the type value. The available values are as follows:

IS_NULL

N/

IS_LONG

Corresponding value. lval

IS_DOUBLE

Corresponding value. dval

IS_STRING

Corresponding value. str

IS_ARRAY

Corresponding value. ht

IS_OBJECT

Corresponding value. obj

IS_BOOL

Corresponding value. lval.

IS_RESOURCE

Corresponding value. lval

Based on this table, we can find two interesting points: first, the PHP array is actually a HashTable, which explains why PHP supports correlated arrays; second, Resource is a long value, it usually stores a pointer, an internal array index, or anything else that only the creator knows can be considered as a handle.

1.1.2 reference count
The reference count is widely used in garbage collection, memory pool, and string. Zend implements a typical reference count. Multiple PHP variables can share the same zval by referencing the counter 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 also increases and decreases accordingly. Once it is reduced to zero, Zend will reclaim the zval.
What about is_ref?

1.1.3 zval status
In PHP, there are two types of variables: Reference and non-reference. They are stored in Zend by reference count. For non-referenced variables, variables must be unrelated. When you modify a variable, other variables cannot be affected, this conflict can be solved using the Copy-On-Write mechanism. If Zend finds that the zval to which the variable points is shared by multiple variables, copy A zval whose refcount is 1 and decrease the refcount of the original zval. This process is called "zval separation ". However, for referenced variables, the requirements are different from those for non-referenced variables. variables that reference values must be bundled. If you modify a variable, all bound variables are modified.
It can be seen that it is necessary to point out the current zval status to cope with these two situations separately. is_ref is for this purpose, it indicates whether all the variables pointing to the zval currently adopt reference assignment -- either all references or none. Modify another variable. Zend will execute Copy-On-Write only when the is_ref of zval is 0, that is, it is not referenced.

1.1.4 zval status Switch
When all the value assignment operations on a zval are referenced or all are non-referenced, an is_ref should be sufficient. However, the world will never be so beautiful, and PHP cannot impose such restrictions on users. When we use both reference and non-Reference Assignment values, we must deal with them in particular.
I. Check the following PHP code:

Copy codeThe Code is as follows: <? Php
$ A = 1;
$ B = $;
$ C = $ B;
$ D = & $ c; // insert a reference in a bunch of non-Reference Values
?>

This code is first initialized, which will create a new zval, is_ref = 0, refcount = 1, and point a to this zval; then there will be two non-reference assignments, as mentioned above, you only need to point B and c to the zval of a. the last row is a reference value assignment, and is_ref must be 1, however, Zend finds that zval pointed to by c is not of the reference type, so it creates a separate zval for c and points d to the zval at the same time.
In essence, this can also be seen as a Copy-On-Write, not just a value, but is_ref is also a protected object.
The entire process is illustrated as follows:

Case II: See the following PHP code:

Copy codeThe Code is as follows: <? Php
$ A = 1;
$ B = & $;
$ C = & $ B;
$ D = $ c; // insert a non-reference in a bunch of reference values
?>

The first three sentences of this Code point a, B, and c to a zval. is_ref = 1, refcount = 3; the fourth sentence is a non-reference value assignment, generally, you only need to increase the reference count. However, the target zval is a reference variable. Simply adding the reference count is obviously incorrect, the Zend solution is to generate a zval copy for d separately.
The entire process is as follows:

1.1.5 parameter transfer
The transfer of PHP function parameters is the same as that of variable assignment. Non-reference transfer is equivalent to non-Reference Assignment. Reference transfer is equivalent to reference assignment, and may also lead to the execution of zval status switching. This will be mentioned later.

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.