每天一个 PHP 语法-变量使用及内部实现

说明#

这里基于 php7.2.5 进行测试,php7 之后内部结构变化应该不是太大,但与 php5.X 有差别。

使用#

变量声明及使用#

$a = "hello"; // string
echo is_string($a); // 1
$b = 1; // int
echo is_int($b); // 1
$c = [1]; // array
$d = true; // bool

弱类型#

区别于 C/JAVA/GO 等强类型语言,PHP 在声明变量的时候不需要指定变量的类型,代码在执行的时候会自动去解析变量的类型。

如何实现#

存储变量的底层结构 zval、zend_value (zend_types.h)

struct _zval_struct {
    zend_value        value;            /* value 变量值放在这里 */
    union {
        struct {
            ZEND_ENDIAN_LOHI_4(
                zend_uchar    type,            /* active type 变量类型  stirng/true/false/array */
                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     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 */
        uint32_t     access_flags;         /* class constant access flags */
        uint32_t     property_guard;       /* single property guard */
        uint32_t     extra;                /* not further specified */
    } u2;
};

// zend_value
typedef union _zend_value {
    zend_long         lval;                /* long value 这里存储int的值 */
    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;


// zval.u1.v1.type的类型的定义
/* regular data types */
#define IS_UNDEF                    0
#define IS_NULL                        1 空 
#define IS_FALSE                    2 false
#define IS_TRUE                        3
#define IS_LONG                        4
#define IS_DOUBLE                    5
#define IS_STRING                    6 字符串
#define IS_ARRAY                    7
#define IS_OBJECT                    8
#define IS_RESOURCE                    9
#define IS_REFERENCE                10

/* constant expressions */
#define IS_CONSTANT                    11
#define IS_CONSTANT_AST                12

/* fake types */
#define _IS_BOOL                    13
#define IS_CALLABLE                    14
#define IS_ITERABLE                    19
#define IS_VOID                        18

/* internal types */
#define IS_INDIRECT                 15
#define IS_PTR                        17
#define _IS_ERROR                    20

我们可以先看一下 zval.u1.v.type 与 zend_value 其他的元素可以暂时忽略。

zval 是存放变量的结构体,变量的值则放在 zend_value 中,zend_value 是一个 union 类型,也就是共用体,特点就是可以存放多种类型的数据,但同时只有一个种类型可以有值。

$a = "hello";

那么 PHP 代码在编译、执行的时候通过词法语法解析,将值 1 生成一个 zval 结构体,zval.u1.type 就是 IS_STRING 类型,值存储在 zend_value 中的 str 也就是 zend_string 中。

总结#

弱类型的实现是基于 zend_value 共用体实现,变量类型在代码解析的时候根据语法去解析生成的。

本作品采用《CC 协议》,转载必须注明作者和本文链接