源码研究: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;

 

转载于:https://www.cnblogs.com/jiujuan/p/8904904.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值