In-depth _php tutorial on PHP variable storage

Source: Internet
Author: User
1.1.1 Zval Structure
Zend uses the ZVAL structure to store the value of the PHP variable, as shown in the following structure:
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; The

Zend determines which member of the value is accessed based on the type value, and the available values are as follows:

is_null

n/a

Is_long

corresponds to value.lval

is_double

corresponds to value.dval

is_string

corresponds to value.str

Is_array

corresponds to value.ht

is_object

corresponds to value.obj

is_bool

corresponds to value.lval.

Is_resource

corresponds to value.lval

Here are two interesting places to find: First, the PHP array is actually a hashtable, which explains why PHP supports associative arrays, and secondly, resource is a long value, which is usually a pointer, An index of an internal array or something that only the creator knows for itself can be regarded as a handle.

1.1.2 Reference count
Reference counting is widely used in places such as garbage collection, memory pools, and strings, and Zend implements a typical reference count. Multiple PHP variables can be shared by using the reference counting mechanism to share the remaining two members of the same zval,zval Is_ref and refcount to support this sharing.
Obviously, RefCount is used for counting, and when the reference is added or subtracted, the value is incremented and decremented accordingly, and once it is reduced to zero, Zend reclaims the zval.
What about Is_ref?

1.1.3 Zval Status
In PHP, there are two types of variables-reference and non-reference, which are stored in Zend in the form of reference counts. For non-reference variables, it is required that the variables are irrelevant, modifying a variable without affecting other variables, and using the copy-on-write mechanism to resolve the conflict-when attempting to write a variable, Zend discovers that the zval that the variable points to is shared by multiple variables. It copies a copy of RefCount 1 Zval and decrements the refcount of the original Zval, which is called "Zval separation". However, for reference variables, which are required to be the opposite of non-reference types, the variables that reference the assignment must be bundled, and modifying a variable modifies all the bundle variables.
Visible, it is necessary to point out the current state of the zval, in order to deal with both cases, IS_REF is the purpose, it points out whether the current pointer to the Zval is the use of reference assignment-or all references, or all is not. At this point, a variable is modified, and Zend executes copy-on-write only if it finds that its zval is_ref is 0, or non-referenced.

1.1.4 zval Status Switch
When all assignment operations on a zval are references or are non-references, a is_ref is sufficient. However, the world will not be so good, PHP can not make this restriction to users, when we mix the use of reference and non-reference assignment, we have to do special processing.
Situation I, see the following PHP code:
Copy the Code code as follows:
$a = 1;
$b = $a;
$c = $b;
$d = & $c; In a bunch of non-reference assignments, insert a reference
?>

This code first initializes, which creates a new zval,is_ref=0, Refcount=1, and points A to the zval, followed by two non-reference assignments, as mentioned above, as long as both B and C point to A's zval, and the last line is a reference assignment. Is_ref is required to be 1, but Zend finds that the zval that C points to is not a reference type, so create a separate zval for C and Point D to that zval.
In essence, this can also be seen as a copy-on-write, not just a value,is_ref but also a protected object.
The entire process is illustrated as follows:

Situation II, see the following PHP code:
Copy the Code code as follows:
$a = 1;
$b = & $a;
$c = & $b;
$d = $c; In a bunch of reference assignments, insert a non-reference
?>

The first three sentences of this code will point A, B and C to a zval, its is_ref=1, refcount=3; the sentence is a non-reference assignment, usually only need to increase the reference count, but the target zval is a reference variable, simply increase the reference count is obviously wrong, The workaround for Zend is to generate a separate copy of the zval for D.
The whole process is as follows:

1.1.5 parameter passing
PHP function parameters are passed in the same way as variable assignments, non-reference passes are equivalent to non-reference assignments, reference passes are equivalent to reference assignments, and can also cause zval state transitions to be performed. This will also be mentioned in the following.

http://www.bkjia.com/PHPjc/327575.html www.bkjia.com true http://www.bkjia.com/PHPjc/327575.html techarticle The 1.1.1 Zval structure Zend uses the ZVAL structure to store the value of the PHP variable, as follows: The copy code code is as follows: TypeDef Union _ZVALUE_VALUE {Long lval;/* Long value */DOUBL E ...

  • 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.