php 常量-标准常量

标准常量的初始化
通过define()函数定义的模块编号都是PHP_USER_CONSTANT这表示是用户定义的常量。除此之外我们在平时使用较多的常量:如错误报告级别E_ALL, E_WARNING等常量就有点不同了。这些是PHP内置定义的常量,他们属于标准常量
在Zend引擎启动后,会执行如下的标准常量注册操作。 php_module_startup() -> zend_startup() -> zend_register_standard_constants()

 1 void zend_register_standard_constants(TSRMLS_D)
 2 {
 3     REGISTER_MAIN_LONG_CONSTANT("E_ERROR", E_ERROR, CONST_PERSISTENT | CONST_CS);
 4     REGISTER_MAIN_LONG_CONSTANT("E_RECOVERABLE_ERROR", E_RECOVERABLE_ERROR, CONST_PERSISTENT | CONST_CS);
 5     REGISTER_MAIN_LONG_CONSTANT("E_WARNING", E_WARNING, CONST_PERSISTENT | CONST_CS);
 6     REGISTER_MAIN_LONG_CONSTANT("E_PARSE", E_PARSE, CONST_PERSISTENT | CONST_CS);
 7     REGISTER_MAIN_LONG_CONSTANT("E_NOTICE", E_NOTICE, CONST_PERSISTENT | CONST_CS);
 8     REGISTER_MAIN_LONG_CONSTANT("E_STRICT", E_STRICT, CONST_PERSISTENT | CONST_CS);
 9     REGISTER_MAIN_LONG_CONSTANT("E_DEPRECATED", E_DEPRECATED, CONST_PERSISTENT | CONST_CS);
10     REGISTER_MAIN_LONG_CONSTANT("E_CORE_ERROR", E_CORE_ERROR, CONST_PERSISTENT | CONST_CS);
11     REGISTER_MAIN_LONG_CONSTANT("E_CORE_WARNING", E_CORE_WARNING, CONST_PERSISTENT | CONST_CS);
12     REGISTER_MAIN_LONG_CONSTANT("E_COMPILE_ERROR", E_COMPILE_ERROR, CONST_PERSISTENT | CONST_CS);
13     REGISTER_MAIN_LONG_CONSTANT("E_COMPILE_WARNING", E_COMPILE_WARNING, CONST_PERSISTENT | CONST_CS);
14     REGISTER_MAIN_LONG_CONSTANT("E_USER_ERROR", E_USER_ERROR, CONST_PERSISTENT | CONST_CS);
15     REGISTER_MAIN_LONG_CONSTANT("E_USER_WARNING", E_USER_WARNING, CONST_PERSISTENT | CONST_CS);
16     REGISTER_MAIN_LONG_CONSTANT("E_USER_NOTICE", E_USER_NOTICE, CONST_PERSISTENT | CONST_CS);
17     REGISTER_MAIN_LONG_CONSTANT("E_USER_DEPRECATED", E_USER_DEPRECATED, CONST_PERSISTENT | CONST_CS);
18 
19     REGISTER_MAIN_LONG_CONSTANT("E_ALL", E_ALL, CONST_PERSISTENT | CONST_CS);
20 
21     REGISTER_MAIN_LONG_CONSTANT("DEBUG_BACKTRACE_PROVIDE_OBJECT", DEBUG_BACKTRACE_PROVIDE_OBJECT, CONST_PERSISTENT | CONST_CS);
22     REGISTER_MAIN_LONG_CONSTANT("DEBUG_BACKTRACE_IGNORE_ARGS", DEBUG_BACKTRACE_IGNORE_ARGS, CONST_PERSISTENT | CONST_CS);
23     /* true/false constants */
24     {
25         zend_constant c;
26     
27         c.flags = CONST_PERSISTENT | CONST_CT_SUBST;
28         c.module_number = 0;
29 
30         c.name = zend_strndup(ZEND_STRL("TRUE"));
31         c.name_len = sizeof("TRUE");
32         c.value.value.lval = 1;
33         c.value.type = IS_BOOL;
34         zend_register_constant(&c TSRMLS_CC);
35         
36         c.name = zend_strndup(ZEND_STRL("FALSE"));
37         c.name_len = sizeof("FALSE");
38         c.value.value.lval = 0;
39         c.value.type = IS_BOOL;
40         zend_register_constant(&c TSRMLS_CC);
41 
42         c.name = zend_strndup(ZEND_STRL("NULL"));
43         c.name_len = sizeof("NULL");
44         c.value.type = IS_NULL;
45         zend_register_constant(&c TSRMLS_CC);
46 
47         c.flags = CONST_PERSISTENT;
48 
49         c.name = zend_strndup(ZEND_STRL("ZEND_THREAD_SAFE"));
50         c.name_len = sizeof("ZEND_THREAD_SAFE");
51         c.value.value.lval = ZTS_V;
52         c.value.type = IS_BOOL;
53         zend_register_constant(&c TSRMLS_CC);
54 
55         c.name = zend_strndup(ZEND_STRL("ZEND_DEBUG_BUILD"));
56         c.name_len = sizeof("ZEND_DEBUG_BUILD");
57         c.value.value.lval = ZEND_DEBUG;
58         c.value.type = IS_BOOL;
59         zend_register_constant(&c TSRMLS_CC);
60     }
61 }

定义了一堆标准常量,REGISTER_MAIN_LONG_CONSTANT()是一个宏,用于注册一个长整形数字的常量,因为C是强类型语言,不同类型的数据等分别处理。在 zend_constants.c和zend_constants.h中有这些宏的声明和定义

posted @ 2018-08-07 17:35  DearMrLi  阅读(169)  评论(0编辑  收藏  举报