源码研究:php变量
2023-09-27 14:24:38 时间
一: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;
相关文章
- PHP操作Redis
- 基于PHP的英语四六级在线模拟考试平台(论文+源码)_kaic
- php--------暂停函数 sleep() 与 usleep() 的区别
- 指定php版本安装composer
- PHP函数,返回多少分钟,多少秒,多少小时前
- PHP统计排行,分页
- PHP源码解析——字符串
- PHP性能神器 —— 生成器
- Windows下PHP服务nginx不能使用file_get_contents的原因
- PHP算法之四大基础算法
- 微信公众平台PHP开发
- php 富文本双引号问题,将双引号变为";html转义入库
- php 常用的第三方插件
- PHP使用redis实现分布式锁
- PHP 源码探秘 - 为什么 trim 会导致乱码
- 67.网络安全渗透测试—[SQL注入篇6]—[MySQL+PHP-读写操作]
- PHP扩展编写、PHP扩展调试、VLD源码分析、基于嵌入式Embed SAPI实现opcode查看
- 关于PHP 源码 加密的 一些 学习(黑刀)
- [转]pHP源码加密方法调查
- nginx安装及配置支持php的教程(全)