php 常量-標準常量

標準常量的初始化
經過define()函數定義的模塊編號都是PHP_USER_CONSTANT這表示是用戶定義的常量。除此以外咱們在平時使用較多的常量:如錯誤報告級別E_ALL, E_WARNING等常量就有點不一樣了。這些是PHP內置定義的常量,他們屬於標準常量
在Zend引擎啓動後,會執行以下的標準常量註冊操做。 php_module_startup() -> zend_startup() -> zend_register_standard_constants()php

 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中有這些宏的聲明和定義函數

相關文章
相關標籤/搜索