每天一个 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 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!