• 源码研究:php变量


    一:php中的变量类型

    1、标量类型:布尔型 bool,整型 int,浮点型 float,字符串型 string
    2、复杂类型:数组 array,对象 object
    3、特殊类型:NULL,资源 resource
    这些变量都是怎么实现的呢?我们都知道php是用 c 语言实现的,那是怎么用c语言实现的呢?
    来看看php5.5.7的源码,看是怎么实现的,最主要的是 zval 这个结构体

    二:zval的定义

    在 zend/zend_types.h
    typedef struct _zval_struct zval

    _zval_struct 这个结构体,是在 zend/zend.h 中定义的

    struct _zval_struct {
        /* Variable information */
        zvalue_value value; /* value */
        zend_uint refcount__gc;
        zend_uchar type; /* active type */
        zend_uchar is_ref__gc;
    };

    _zval_struct 结构体里面有个 zvalue_value value 这个就是变量存储的值,
    zend_uchar type 这个就是变量的类型,判断一个变量是什么类型,就是通过这个type来判断的


    zvalue_value
    又是什么类型的呢?它是一个联合体,定义如下:

    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;

    看见没, 用一个联合体就把php中的数据类型都定义出来了

    zend_uchar type
    变量类型定义, 在zend.h 中,定义了下面几种类型:

    #define IS_NULL 0
    #define IS_LONG 1
    #define IS_DOUBLE 2
    #define IS_BOOL 3
    #define IS_ARRAY 4
    #define IS_OBJECT 5
    #define IS_STRING 6
    #define IS_RESOURCE 7
    #define IS_CONSTANT 8
    #define IS_CONSTANT_ARRAY 9
    #define IS_CALLABLE 10

    zend_uint refcount__gc
    这个跟变量的垃圾回收有关, php5.2 等以及以前用引用计数来进行垃圾回收,php5.3 以后引入了新的垃圾回收算法Concurrent Cycle Collection in Reference Counted Systems,这个解决了循环引用的问题

    其他的zend_uchar,zend_uint 等都是封装 c 语言里面的类型
    zend/zend_types.h 中定义

    typedef unsigned char zend_bool;
    typedef unsigned char zend_uchar;
    typedef unsigned int zend_uint;
    typedef unsigned long zend_ulong;
    typedef unsigned short zend_ushort;
    == just do it ==
  • 相关阅读:
    APP测试-流量测试
    APP测试-流畅度测试
    APP测试-耗电分析
    工具安装-Homebrew
    工具安装-go for Mac
    APP测试-耗电量测试
    APP测试-CPU测试
    APP测试-内存测试
    APP测试-monkey
    APP测试-adb命令
  • 原文地址:https://www.cnblogs.com/jiujuan/p/8904904.html
Copyright © 2020-2023  润新知