|  | 
|  | 1 | +## 7.8 常量 | 
|  | 2 | +常量的具体实现前面章节已经介绍过,这里不再重复。PHP提供了很多用于常量注册的宏,可以在扩展的`PHP_MINIT_FUNCTION()`中定义: | 
|  | 3 | +```c | 
|  | 4 | +//注册NULL常量 | 
|  | 5 | +#define REGISTER_NULL_CONSTANT(name, flags) \ | 
|  | 6 | + zend_register_null_constant((name), sizeof(name)-1, (flags), module_number) | 
|  | 7 | + | 
|  | 8 | +//注册bool常量 | 
|  | 9 | +#define REGISTER_BOOL_CONSTANT(name, bval, flags) \ | 
|  | 10 | + zend_register_bool_constant((name), sizeof(name)-1, (bval), (flags), module_number) | 
|  | 11 | + | 
|  | 12 | +//注册整形常量 | 
|  | 13 | +#define REGISTER_LONG_CONSTANT(name, lval, flags) \ | 
|  | 14 | + zend_register_long_constant((name), sizeof(name)-1, (lval), (flags), module_number) | 
|  | 15 | + | 
|  | 16 | +//注册浮点型常量 | 
|  | 17 | +#define REGISTER_DOUBLE_CONSTANT(name, dval, flags) \ | 
|  | 18 | + zend_register_double_constant((name), sizeof(name)-1, (dval), (flags), module_number) | 
|  | 19 | + | 
|  | 20 | +//注册字符串常量,str类型为char* | 
|  | 21 | +#define REGISTER_STRING_CONSTANT(name, str, flags) \ | 
|  | 22 | + zend_register_string_constant((name), sizeof(name)-1, (str), (flags), module_number) | 
|  | 23 | + | 
|  | 24 | +//注册字符串常量,截取指定长度,str类型为char* | 
|  | 25 | +#define REGISTER_STRINGL_CONSTANT(name, str, len, flags) \ | 
|  | 26 | + zend_register_stringl_constant((name), sizeof(name)-1, (str), (len), (flags), module_number) | 
|  | 27 | +``` | 
|  | 28 | +除了上面这些还有`REGISTER_NS_XXX`系列的宏用于带namespace的常量注册,另外如果这些类型不能满足需求,则可以通过`zend_register_constant(zend_constant *c)`注册,比如常量类型为数组。 | 
|  | 29 | +```c | 
|  | 30 | +PHP_MINIT_FUNCTION(mytest) | 
|  | 31 | +{  | 
|  | 32 | + ... | 
|  | 33 | +
 | 
|  | 34 | + REGISTER_STRING_CONSTANT("MY_CONS_1", "this is a constant", CONST_CS | CONST_PERSISTENT); | 
|  | 35 | +} | 
|  | 36 | +``` | 
|  | 37 | +```php | 
|  | 38 | +echo MY_CONS_1; | 
|  | 39 | +=========[output]========= | 
|  | 40 | +this is a constant | 
|  | 41 | +``` | 
|  | 42 | +如果在扩展中需要用到其他扩展或内核定义的常量,则可以通过以下函数获取常量的值: | 
|  | 43 | +```c | 
|  | 44 | +ZEND_API zval *zend_get_constant(zend_string *name); | 
|  | 45 | +ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len); | 
|  | 46 | +``` | 
0 commit comments