php简单的管理系统操作(php语法基础知识)

说明

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

使用

变量声明及使用

$a = \"hello\"; // stringecho is_string($a); // 1$b = 1; // intecho 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_valuetypedef 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_UNDEF0#define IS_NULL1 空 #define IS_FALSE2 false#define IS_TRUE3#define IS_LONG4#define IS_DOUBLE5#define IS_STRING6 字符串#define IS_ARRAY7#define IS_OBJECT8#define IS_RESOURCE9#define IS_REFERENCE10/* constant expressions */#define IS_CONSTANT11#define IS_CONSTANT_AST12/* fake types */#define _IS_BOOL13#define IS_CALLABLE14#define IS_ITERABLE19#define IS_VOID18/* internal types */#define IS_INDIRECT             15#define IS_PTR17#define _IS_ERROR20

我们可以先看一下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共用体实现,变量类型在代码解析的时候根据语法去解析生成的。

(0)
小多多的头像小多多创始人

相关推荐

发表回复

登录后才能评论