Deep understanding of PHP kernel (5) variables and data types-structure and type of variables, and deep understanding of the kernel
Link: http://www.orlion.ga/238/
Programming Language types can be divided into strong type and weak type. PHP is a weak type language, but C is a strong type language. Internally implemented in PHP on the official website, all variables are saved using the same data structure (zval). This structure indicates various data types in PHP. It not only contains the value of the variable, it also contains the type of the variable. This is the core of the weak PHP type.
Then how does the zval structure implement the weak type?
I. PHP variable types and Storage Structure
PHP does not need to specify the data type when declaring and using variables. PHP is a weak language,PHP has a type,PHP has eight data types, which can be divided into three types: scalar type: boolean, integer, float (double) string; compound type: array, object; special type: resource, NULL
How does the C language implement the weak type in PHP?
1. Variable Storage Structure
The value of the variable is stored in the zval struct as follows. The zval struct is defined in the Zend/zend. h file. Its structure is as follows:
typedef struct _zval_struct zval;...struct _zval_struct { /* Variable information */ zvalue_value value; /* value */ zend_uint refcount__gc; zend_uchar type; /* active type */ zend_uchar is_ref__gc;};
The zval struct contains four fields, which are described as follows:
Attribute name |
Description |
Default Value |
Refcount_gc |
Reference count |
1 |
Is_ref_gc |
Indicates whether it is a reference. |
0 |
Value |
Store variable values |
|
Type |
Variable type |
|
After PHP5.3, A New garbage collection mechanism was introduced. The reference count and referenced field names were changed to refcout_gc and is_ref_gc before which they were refcount and is_ref.
The value of the variable is stored in the zvalue_value structure. The value storage is described below.
2. Variable type
The type field of the zval struct is the most critical field to implement weak types. The type value can be IS_NULL, IS_BOOL, IS_LONG, IS_DOUBLE, IS_STRING, IS_OBJECT, and IS_RESOURCE. The types defined with them are IS_CONSTANT and IS_CONSTANT_ARRAY.
2. variable value Storage
The variable values are stored in the zvalue_value consortium. The struct is defined 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;
Different types of data store variable values using different methods. The corresponding value assignment method is as follows:
General type:
Variable type |
Macro |
|
Boolean |
ZVAL_BOOL |
The variable values of the Boolean OR Integer type are stored in (zval). value. lval. Store with the corresponding IS _ * |
Integer |
ZVAL_LONG |
Float |
ZVAL_DOUBLE |
Z_TYPE_P (z) = IS_BOOL/LONG; Z_LVAL_P (z) = (B )! = 0; |
Null |
ZVAL_NULL |
Variable values of NULL values do not need to be stored. You only need to mark (zval). type as IS_NUL Z_TYPE_P (z) = IS_NULL; |
Resource |
ZVAL_RESOURCE |
Resource-type storage is similar to other common variables, but its initialization and storage implementation are not Same as Z_TYPE_P (z) = IS_RESOURCE; Z_LVAL_P (z) = 1; |
String
The string type is the same as other data types, but a string length field is added when the string is stored.
struct { char *val; int len;} str;
(The string length is stored because the string operation is very frequent, which helps to save time. It is the practice of changing the space time)
Array
Arrays are the most common and powerful variable types in PHP. The value of the array is stored in the zvalue_value.ht field, which is a HashTable type data. The PHP array uses a hash table to store associated data. PHP uses two data structures: HashTable and Bucket. All PHP work is implemented by a hash table.
Object
PHP objects are compound types of data stored using a zend_object_value struct, which is defined as follows:
Typedef struct _ zend_object_value {zend_object_handle handle; // The index zend_object_handlers * handlers;} zend_object_value of the unsigned int type, EG (objects_store). object_buckets;
PHP objects can only be created at runtime. The EG macro is described earlier. This is a global struct because of the data stored at runtime. This includes the object pool (EG (objects_store) used to store all created objects. The zend_object_handle field of the object value content is the index of the current object in the object pool, the handlers field stores the processing functions when the object is operated.
The implementation of weak variable containers in PHP is inclusive.