In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the PHP variable storage example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
1.1.1 zval structure
Zend uses the zval structure to store the value of the PHP variable, which is as follows:
The copy 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 decides which member of value to access based on the type value. 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
Corresponding to value.lval.
IS_RESOURCE
Corresponding value.lval
According to this table, you can find two interesting things: first, the array of PHP is actually a HashTable, which explains why PHP can support associative arrays; second, Resource is a long value, which usually holds a pointer, an index of an internal array, or something that only the creator knows about, which can be thought of as a handle.
1.1.2 reference count
Reference counting is widely used in garbage collection, memory pools, and strings, and Zend implements a typical reference count. Multiple PHP variables can be shared through the reference counting mechanism, and the remaining two members of the same zval,zval, is_ref and refcount, are used to support this sharing.
Obviously, refcount is used for counting, and when you increase or decrease references, this value increases and decreases accordingly, and once it decreases to zero, Zend reclaims the zval.
What about is_ref?
1.1.3 zval status
In PHP, there are two types of variables-referenced and non-referenced, both of which are stored in Zend by reference counting. For non-referenced variables, it is required that the variables are independent of each other, and when one variable is modified, the other variables can not be affected. This conflict can be resolved by using the Copy-On-Write mechanism. When trying to write a variable, if Zend finds that the zval that the variable points to is shared by multiple variables, it copies a zval with a refcount of 1 for it and decrements the refcount of the original zval. This process is called "zval separation". However, for referential variables, contrary to non-referential variables, variables that reference assignments must be bundled, and modifying one variable modifies all bundled variables.
Obviously, it is necessary to point out the state of the current zval to deal with these two situations separately, and is_ref is for this purpose, indicating whether all current variables pointing to the zval are assigned by references-- all references or none at all. At this point, modify another variable, and Zend will execute Copy-On-Write only if it is found that the is_ref of its zval is 0, that is, it is not a reference.
1.1.4 zval status switching
When all assignments on a zval are references or non-references, an is_ref is sufficient. However, the world is not always so beautiful, and PHP cannot impose such restrictions on users, and when we mix reference and non-reference assignments, we have to do something special.
Case I. look at the following PHP code:
The copy code is as follows:
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 earlier, just point b and c to a zval; the last line is a reference assignment that requires an is_ref of 1, but Zend finds that the zval pointed to by c is not referenced, so 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 a value,is_ref but also a protected object.
The whole process is illustrated as follows:
In the case of II, see the following PHP code:
The copy code is as follows:
The first three sentences of this code will point a, b and c to a zval, its is_ref=1, the fourth sentence of refcount=3; is a non-reference assignment, usually only need to increase the reference count, but the target zval belongs to the reference variable, simply increasing the reference count is obviously wrong, the solution of Zend is to generate a separate copy of zval for d.
The whole process is as follows:
1.1.5 Parameter transfer
The passing of PHP function parameters is the same as variable assignment, non-reference passing is equivalent to non-reference assignment, reference passing is equivalent to reference assignment, and may also lead to zval state switching. This will be mentioned later.
Thank you for reading this article carefully. I hope the article "sample Analysis of PHP variable Storage" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.