Deep into the PHP variable storage _php Tips

Source: Internet
Author: User
Tags garbage collection

1.1.1 Zval Structure
Zend uses the ZVAL structure to store the values of PHP variables, which are as follows:

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;


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 of value is accessed based on the type value, and the available values are as follows:

Is_null

N/A

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

Corresponds to value.lval.

Is_resource

corresponding value.lval

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

1.1.2 Reference count
Reference counts are widely used in garbage collection, memory pools, and strings, and Zend a typical reference count. Multiple PHP variables can be used to share the remaining two members of the same zval,zval by reference counting mechanism is_ref and refcount to support this sharing.
Obviously, the refcount is used for counting, and when the reference is added or subtracted, the value is incremented and decremented accordingly, and Zend will reclaim the Zval once it is reduced to zero.
So what about Is_ref?

1.1.3 Zval State
In PHP, there are two types of variables--references and unreferenced, which are stored in reference counting in Zend. For unreferenced variables, it requires that the variables are irrelevant, and when modifying a variable, the other variables cannot be affected, and the copy-on-write mechanism is used to resolve the conflict-when an attempt is made to write a variable, Zend finds that the zval that the variable points to is shared by multiple variables, It copies a copy of the zval of RefCount 1 and decrements the refcount of the original Zval, which is called "Zval separation." However, for reference variables, in contrast to the unreferenced type, the variables that reference the assignment must be bundled, and modifying a variable modifies all the bundle variables.
It is obvious that it is necessary to point out the state of the current zval to deal with both cases, and is_ref is to indicate whether all the current variables pointing to the Zval are assigned by reference-either all references or none at all. To modify a variable at this point, the Zend executes copy-on-write only if it finds that its zval is_ref is 0, that is, not a reference.

1.1.4 Zval State Switching
When all assignment operations on a zval are references or are unreferenced, a is_ref is sufficient to handle. However, the world will not be so beautiful, PHP can not make this restriction on users, when we mix the use of reference and unreferenced assignment, we have to do special processing.
Situation I, look like the following PHP code:

Copy Code code as follows:

<?php
$a = 1;
$b = $a;
$c = $b;
$d = & $c; In a heap of unreferenced assignments, insert a reference
?>

This code is initialized first, this creates a new zval,is_ref=0, Refcount=1, and points A to the zval, followed by two unreferenced assignments, as mentioned earlier, where both B and C point to the zval of a and the last line is a reference assignment, Requires Is_ref to be 1, but Zend finds that C-pointing zval is not a reference type, 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 only value,is_ref but also protected objects.
the entire process is illustrated as follows:

Situation II, see the following PHP code:

Copy Code code as follows:

<?php
$a = 1;
$b = & $a;
$c = & $b;
$d = $c; In a bunch of reference assignments, insert a non-referenced
?>

The first three sentences of this code will point A, B, and C to a zval, its is_ref=1, refcount=3; the third sentence is an unreferenced assignment, which usually requires only an increase in the reference count, whereas the target zval belongs to the reference variable, and simply increasing the reference count is obviously wrong. The Zend solution is to generate a separate copy of Zval for D.
The whole process looks like this:

1.1.5 parameter Pass
The transfer of PHP function parameters is the same as the variable assignment, which is equivalent to unreferenced assignment, which is equivalent to a reference assignment, and may also result in the execution of a zval state switch. This will also 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.