首页 > 代码库 > php扩展开发-常量

php扩展开发-常量

//常量在内核中的结构
typedef struct _zend_constant {
    zval value;
    int flags;
    char *name;
    uint name_len;
    int module_number;
} zend_constant;
//常量注册的宏定义
#define
REGISTER_NULL_CONSTANT(name, flags) zend_register_null_constant((name), sizeof(name), (flags), module_number TSRMLS_CC) #define REGISTER_BOOL_CONSTANT(name, bval, flags) zend_register_bool_constant((name), sizeof(name), (bval), (flags), module_number TSRMLS_CC) #define REGISTER_LONG_CONSTANT(name, lval, flags) zend_register_long_constant((name), sizeof(name), (lval), (flags), module_number TSRMLS_CC) #define REGISTER_DOUBLE_CONSTANT(name, dval, flags) zend_register_double_constant((name), sizeof(name), (dval), (flags), module_number TSRMLS_CC) #define REGISTER_STRING_CONSTANT(name, str, flags) zend_register_string_constant((name), sizeof(name), (str), (flags), module_number TSRMLS_CC) #define REGISTER_STRINGL_CONSTANT(name, str, len, flags) zend_register_stringl_constant((name), sizeof(name), (str), (len), (flags), module_number TSRMLS_CC)
//在MINIT函数中定义常量
//参数说明:
//1,常量名称,此处只能是字符串,不能是变量
//2,常量的值
//3,常量的属性,对应结构体
zend_constant的int flags。CONST_CS表示区分大小写,CONST_PERSISTENT表示在php运行过程中,长期存在
PHP_MINIT_FUNCTION(myext)
{
   REGISTER_STRING_CONSTANT("MYEXT_VERSION",PHP_MYEXT_VERSION,CONST_CS|CONST_PERSISTENT);
    REGISTER_LONG_CONSTANT("MYEXT_LONG_ZERO",0,CONST_CS|CONST_PERSISTENT);
    return SUCCESS;
}
/*

var_dump(MYEXT_VERSION);
var_dump(MYEXT_LONG_ZERO);

 

string(3) "1.0"

int(0)

*/

 

php扩展开发-常量