php内核-数据类型

迈不过友情╰ 2022-02-13 13:53 349阅读 0赞

结构体&联合体

在这里插入图片描述
结构体内每一个变量都独占一块内存,并保持内存对齐
在这里插入图片描述
联合体内所有变量公用同一块内存,占用内存大小为联合体中占用内存最大的变量的内存大小

zval

zval定义

每个变量的结构定义

  1. struct _zval_struct {
  2. zend_value value; /* value 变量值*/
  3. union {
  4. struct {
  5. ZEND_ENDIAN_LOHI_3(
  6. zend_uchar type, /* active type 变量类型 */
  7. zend_uchar type_flags, /* 类型掩码,各类型会有几个不同的属性值,用于内存管理*/
  8. union {
  9. uint16_t call_info; /* call info for EX(This) */
  10. uint16_t extra; /* not further specified */
  11. } u)
  12. } v;
  13. uint32_t type_info;
  14. } u1;
  15. union {
  16. uint32_t next; /* hash collision chain */
  17. uint32_t cache_slot; /* cache slot (for RECV_INIT) */
  18. uint32_t opline_num; /* opline number (for FAST_CALL) */
  19. uint32_t lineno; /* line number (for ast nodes) */
  20. uint32_t num_args; /* arguments number for EX(This) */
  21. uint32_t fe_pos; /* foreach position */
  22. uint32_t fe_iter_idx; /* foreach iterator index */
  23. uint32_t access_flags; /* class constant access flags */
  24. uint32_t property_guard; /* single property guard */
  25. uint32_t constant_flags; /* constant flags */
  26. uint32_t extra; /* not further specified */
  27. } u2;
  28. };
  1. zend_value表示变量的值
  2. u1表示变量的类型
  3. u2用于辅助功能

zend_value定义

  1. typedef union _zend_value {
  2. zend_long lval; /* 整型 */
  3. double dval; /* 浮点型 */
  4. zend_refcounted *counted; /* 获取不同类型结构的gc头部 */
  5. zend_string *str; /* 字符型 */
  6. zend_array *arr; /* array数组 */
  7. zend_object *obj; /* object对象 */
  8. zend_resource *res; /* resource类型 */
  9. zend_reference *ref; /* 引用类型 */
  10. zend_ast_ref *ast;
  11. zval *zv;
  12. void *ptr;
  13. zend_class_entry *ce; /* 类 */
  14. zend_function *func; /* 方法 */
  15. struct {
  16. uint32_t w1;
  17. uint32_t w2;
  18. } ww;
  19. } zend_value;

1. 整型

在一个x86_64的机器中,整型的定义typedef int64_t zend_long;
如果是32位的机器中,整型的定义typedef int32_t zend_long;

2. 浮点型

从定义可以看出,就是double类型

3. 字符串-string

字符串的基本定义为:

  1. struct _zend_string {
  2. zend_refcounted_h gc; /* 用于gc */
  3. zend_ulong h; /* hash value */
  4. size_t len; /* 字符串长度 */
  5. char val[1]; /* 字符串的实际内容,默认数组为1 */
  6. };

4. 数组-array

数组的基本定义

  1. struct _zend_array {
  2. zend_refcounted_h gc;
  3. union {
  4. struct {
  5. ZEND_ENDIAN_LOHI_4(
  6. zend_uchar flags,
  7. zend_uchar _unused,
  8. zend_uchar nIteratorsCount,
  9. zend_uchar _unused2)
  10. } v;
  11. uint32_t flags;
  12. } u;
  13. uint32_t nTableMask;
  14. Bucket *arData; /* 数组中实际存储的数据节点 */
  15. uint32_t nNumUsed; /* 数组中使用的个数 */
  16. uint32_t nNumOfElements; /* 数组中元素个数 */
  17. uint32_t nTableSize; /* 数组大小 */
  18. uint32_t nInternalPointer;
  19. zend_long nNextFreeElement;
  20. dtor_func_t pDestructor;
  21. };

Bucket结构,数组中的每个元素的实际存储结构

  1. typedef struct _Bucket {
  2. zval val;
  3. zend_ulong h; /* hash value (or numeric index) */
  4. zend_string *key; /* string key or NULL for numerics */
  5. } Bucket;

变量数据类型的表示(u1.v.type)

  1. #define IS_UNDEF 0
  2. #define IS_NULL 1
  3. #define IS_FALSE 2
  4. #define IS_TRUE 3
  5. #define IS_LONG 4
  6. #define IS_DOUBLE 5
  7. #define IS_STRING 6
  8. #define IS_ARRAY 7
  9. #define IS_OBJECT 8
  10. #define IS_RESOURCE 9
  11. #define IS_REFERENCE 10

发表评论

表情:
评论列表 (有 0 条评论,349人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PHP数据类型

    数据类型是针对各种(变量)值而言,而不是针对变量本身。 PHP有三大数据类型:标量,复合,特殊 标量数据类型(简单数据类型)   整型:int或者integer,整数