Zval是PHP中最重要的數(shù)據(jù)結構之一,它包含了PHP中變量的值和類型相關信息。 zval結構比較簡單, 由三個部分組成: 為了更直觀的看下zval的結構及核心字段的取值,我們上張圖。 對u1.v.type的說明: zval的中zend_value定義如下: 在一個zval中: 所以一個zval占用16字節(jié)。相應php5中,一個zval的大小為48字節(jié),的確是巨大的提升。 可根據(jù)zval.u1.v.type直接區(qū)分,無需zend_value參與 直接存儲在zend_value的lval或dval中。 使用zend_value對應的指針,指向其具體的結構。 比如字串類型的結構為 一個字串變量內存組織如下圖所示, zval.value.str指向zend_string結構。 Zval是PHP中最重要的數(shù)據(jù)結構之一,它包含了PHP中變量的值和類型相關信息。 推薦學習:《PHP7教程》
1. zval
1.1 zval的結構(zend_types.h)
typedef struct _zval_struct zval;struct _zval_struct { zend_value value; /* value */ union { struct { ZEND_ENDIAN_LOHI_4( zend_uchar type, /* active type */ zend_uchar type_flags, zend_uchar const_flags, zend_uchar reserved) /* call info for EX(This) */ } v; uint32_t type_info; } u1; union { uint32_t var_flags; uint32_t next; /* hash collision chain */ uint32_t cache_slot; /* literal cache slot */ uint32_t lineno; /* line number (for ast nodes) */ uint32_t num_args; /* arguments number for EX(This) */ uint32_t fe_pos; /* foreach position */ uint32_t fe_iter_idx; /* foreach iterator index */ } u2;};
1.2 zend_value
typedef union _zend_value { zend_long lval; /* long value */ double dval; /* double value */ zend_refcounted *counted; zend_string *str; zend_array *arr; zend_object *obj; zend_resource *res; zend_reference *ref; zend_ast_ref *ast; zval *zv; void *ptr; zend_class_entry *ce; zend_function *func; struct { uint32_t w1; uint32_t w2; } ww; } zend_value;
1.3 zval內存占用
2. 變量存儲
2.1 true, false, null
2.2 long,double
2.3 其它類型(string,array,object,resource等)
struct _zend_string { zend_refcounted_h gc; zend_ulong h; /* hash value */ size_t len; char val[1]; };