1,创建一个新的扩展
./ext_skel --extname=inis
2,宏
#define ZEND_DECLARE_MODULE_GLOBALS(module_name) ts_rsrc_id module_name##_globals_id;
#define STD_PHP_INI_ENTRY STD_ZEND_INI_ENTRY
#define STD_ZEND_INI_ENTRY(name, default_value, modifiable, on_modify, property_name, struct_type, struct_ptr) \
ZEND_INI_ENTRY2(name, default_value, modifiable, on_modify, (void *) XtOffsetOf(struct_type, property_name), (void *) &struct_ptr##_id)
#define ZEND_INI_ENTRY3_EX(name, default_value, modifiable, on_modify, arg1, arg2, arg3, displayer) \
{ 0, modifiable, name, sizeof(name), on_modify, arg1, arg2, arg3, default_value, sizeof(default_value)-1, NULL, 0, 0, 0, displayer },
#define ZEND_INI_BEGIN() static const zend_ini_entry ini_entries[] = {
#define ZEND_INI_END() { 0, 0, NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, 0, 0, NULL } };
//注册ini变量,在minit中调用
#define REGISTER_INI_ENTRIES() zend_register_ini_entries(ini_entries, module_number TSRMLS_CC)
//清除ini变量,在mshutdown中调用
#define UNREGISTER_INI_ENTRIES() zend_unregister_ini_entries(module_number TSRMLS_CC)
//在phpinfo中显示变量值,在minfo中调用
#define DISPLAY_INI_ENTRIES() display_ini_entries(zend_module)
//
3,定义ini变量
struct _zend_ini_entry {
int module_number;
int modifiable;
char *name;
uint name_length;
ZEND_INI_MH((*on_modify));
void *mh_arg1;
void *mh_arg2;
void *mh_arg3;
char *value;
uint value_length;
char *orig_value;
uint orig_value_length;
int orig_modifiable;
int modified;
void (*displayer)(zend_ini_entry *ini_entry, int type);
};
//第二个参数即为ini变量的初始值
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("inis.enable", "1", PHP_INI_ALL, OnUpdateBool, enable, zend_inis_globals, inis_globals)
STD_PHP_INI_ENTRY("inis.name", "", PHP_INI_ALL, OnUpdateString, name, zend_inis_globals, inis_globals)
STD_PHP_INI_ENTRY("inis.level", "0", PHP_INI_ALL, OnUpdateLong, level, zend_inis_globals, inis_globals)
PHP_INI_END()
#展开后
static const zend_ini_entry ini_entries[] = {//BEGIN
{0, PHP_INI_ALL, "inis.enable", sizeof("inis.enable"), OnUpdateBool, enable, zend_inis_globals, inis_globals... }//inis.enable定义
...
{ 0, 0, NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, 0, 0, NULL } //END
};
/*初始化ini值的宏*/
ZEND_INIT_MODULE_GLOBALS(module_name, globals_ctor, globals_dtor)
#module_name 与传递给ZEND_BEGIN_MODULE_GLOBALS()宏相同的扩展名称。
#globals_ctor 构造函数指针。在myfile扩展里,函数原形与void php_myfile_init_globals(zend_myfile_globals *myfile_globals)类似
#globals_dtor 析构函数指针。例如,php_myfile_init_globals(zend_myfile_globals *myfile_globals)
4,注册/清除/显示 ini变量
PHP_MINIT_FUNCTION(inis)
{
REGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(inis)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_MINFO_FUNCTION(inis)
{
php_info_print_table_start();
php_info_print_table_header(2, "inis support", "enabled");
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
5,获取ini变量的值
//INIS_G宏即是用于获取ini变量的值的方法
PHP_FUNCTION(inis_report) {
php_printf("Name:%s\n", INIS_G(name));
php_printf("Level:%d\n", INIS_G(level));
}
6,修改ini变量
#编辑php.ini文件
vi /etc/php/php.ini
#添加动态库,并修改其默认值
extension=inis.so
inis.name = leon123
inis.level = 200
#运行,ini变量的值变成为上面修改过的值
kentchentekiiMac-23868:inis kentchen$ php -q inis.php
Functions available in the test extension:
inis_report
Name:leon123
Level:200
#注释php.ini里设置值的语句,ini变量值变回了默认值
kentchentekiiMac-23868:inis kentchen$ php -q inis.php
Functions available in the test extension:
inis_report
Name:leon peng
Level:100