PHP扩展中使用php.ini中的配置

森森 2009-10-06 92 views

编写PHP扩展的时候使用PHP自带的ext_skel生成一个模板文件,里面会包含一些初始设置和注释,可以方便初学者搞懂一些基础知识

这里讲讲编写扩展的时候如何读取在php.ini中设置的值,这里以testini扩展为例

./ext_skel –extname=testini

打开testini.c会看到有一下几行代码:

/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("testini.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_testini_globals, testini_globals)
    STD_PHP_INI_ENTRY("testini.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_testini_globals, testini_globals)
PHP_INI_END()
*/
/* }}} */

可以看出,这段代码默认是被注释掉了的,要使用他,先把注释去掉:

PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("testini.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_testini_globals, testini_globals)
    STD_PHP_INI_ENTRY("testini.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_testini_globals, testini_globals)
PHP_INI_END()

然后把STD_PHP_INI_ENTRY(“test………)替换成PHP_INI_ENTRY(“testini………”,      “42″, PHP_INI_ALL, NULL),即:

PHP_INI_BEGIN()
    PHP_INI_ENTRY("testini.global_value",      "42", PHP_INI_ALL, NULL)
    PHP_INI_ENTRY("testini.global_string", "foobar", PHP_INI_ALL, NULL)
PHP_INI_END()

然后找到如下代码:

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(testini)
{
 /* If you have INI entries, uncomment these lines
 REGISTER_INI_ENTRIES();
 */
 return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(testini)
{
 /* uncomment this line if you have INI entries
 UNREGISTER_INI_ENTRIES();
 */
 return SUCCESS;
}
/* }}} */

去掉里面的注释变成:

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(testini)
{
  REGISTER_INI_ENTRIES();

 return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(testini)
{
  UNREGISTER_INI_ENTRIES();

 return SUCCESS;
}
/* }}} */

好了,再在你的php.ini设置
testini.global_string = test
testini.global_value = 1234

这样,便可以通过INI_INT、INI_STR等获取值了,比如:INI_STR(“testini.global_string”)

Tags: , , , / Posted in PHP扩展
欢迎订阅: Google Reader | 鲜果 | 抓虾 | 九点 | QQ邮箱 | 有道 | 更多

评论暂缺

  • (Required)
  • (Required, will not be published)

友情链接

Design By CKSKY TEAM VERSION 3.0 他说博客不用备案... © Copyright 2010 All Rights Reserved