内部函数与用户自定义函数冲突,用户无法在PHP代码中覆盖内部函数,执行PHP脚本时会提示error错误。
内部函数的定义非常简单,我们只需要创建一个普通的C函数,然后创建一个zend_internal_function结构添加到 EG(function_table)__ (也可能是CG(function_table),取决于在哪一阶段注册)中即可使用,内部函数 通常__ 情况下是在php_module_startup阶段注册的,这里之所以说通常是按照标准的扩展定义,除了扩展提供的方式我们可以在任何阶段自由定义内部函数,当然并不建议这样做。下面我们先不讨论扩展标准的定义方式,我们先自己尝试下如何注册一个内部函数。
根据zend_internal_function的结构我们知道需要定义一个handler:
void qp_test(INTERNAL_FUNCTION_PARAMETERS)
{
printf("call internal function 'qp_test'\n");
}
然后创建一个内部函数结构(我们在扩展PHP_MINIT_FUNCTION方法中注册,也可以在其他位置):
PHP_MINIT_FUNCTION(xxxxxx)
{
zend_string *lowercase_name;
zend_function *reg_function;
//函数名转小写,因为php的函数不区分大小写
lowercase_name = zend_string_alloc(7, 1);
zend_str_tolower_copy(ZSTR_VAL(lowercase_name), "qp_test", 7);
lowercase_name = zend_new_interned_string(lowercase_name);
reg_function = malloc(sizeof(zend_internal_function));
reg_function->internal_function.type = ZEND_INTERNAL_FUNCTION; //定义类型为内部函数
reg_function->internal_function.function_name = lowercase_name;
reg_function->internal_function.handler = qp_test;
zend_hash_add_ptr(CG(function_table), lowercase_name, reg_function); //注册到CG(function_table)符号表中
}
接着编译、安装扩展,测试:
qp_test();
结果输出:
call internal function 'qp_test'
这样一个内部函数就定义完成了。这里有一个地方需要注意的我们把这个函数注册到 CG(function_table) 中去了,而不是 EG(function_table) ,这是因为在php_request_startup阶段会把 CG(function_table) 赋值给 EG(function_table) 。
上面的过程看着比较简单,但是在实际应用中不要这样做,PHP提供给我们一套标准的定义方式,接下来看下如何在扩展中按照官方方式提供一个内部函数。
首先也是定义C函数,这个通过PHP_FUNCTION宏定义:
PHP_FUNCTION(qp_test)
{
printf("call internal function 'qp_test'\n");
}
然后是注册过程,这个只需要我们将所有的函数数组添加到扩展结构zend_module_entry.functions即可,扩展加载过程中会自动进行函数注册(见1.2节),不需要我们干预:
const zend_function_entry xxxx_functions[] = {
PHP_FE(qp_test, NULL)
PHP_FE_END
};
zend_module_entry xxxx_module_entry = {
STANDARD_MODULE_HEADER,
"扩展名称",
xxxx_functions,
PHP_MINIT(timeout),
PHP_MSHUTDOWN(timeout),
PHP_RINIT(timeout), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(timeout), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(timeout),
PHP_TIMEOUT_VERSION,
STANDARD_MODULE_PROPERTIES
};
关于更多扩展中函数相关的用法会在后面扩展开发一章中详细介绍,这里不再展开。
邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453
Copyright © 2015-2024
备案号:京ICP备15003423号-3