常量的内部结构
Zend/zend_constants.htypedef struct _zend_constant { zval value; /* zval结构,PHP内部变量的存储结构 */ int flags; /* 常量的标记如 CONST_PERSISTENT | CONST_CS */ char *name; /* 常量名称 */ uint name_len; /* 名称长度 */ int module_number; /* 模块号 */} zend_constant;
php的define函数
Zend/zend_builtin_functions.c
c.value = *val;zval_copy_ctor(&c.value);if (val_free) { zval_ptr_dtor(&val_free);}c.flags = case_sensitive; /* non persistent */c.name = zend_strndup(name, name_len);c.name_len = name_len+1;c.module_number = PHP_USER_CONSTANT;if (zend_register_constant(&c TSRMLS_CC) == SUCCESS) { RETURN_TRUE;} else { RETURN_FALSE;}
php 的defined函数
if (zend_get_constant_ex(name, name_len, &c, NULL, ZEND_FETCH_CLASS_SILENT TSRMLS_CC)) { zval_dtor(&c); RETURN_TRUE;} else { RETURN_FALSE;}
zend_get_constant_ex调用zend_get_constantzend_hash_find(EG(zend_constants), name, name_len+1, (void **) &c)
php注册常量的步骤
php_cgi_startup() -> php_module_startup() ->zend_startup() -> zend_register_standard_constants()->zend_register_constant()
zend_hash_add(EG(zend_constants), name, c->name_len, (void *) c, sizeof(zend_constant), NULL)==FAILURE)