2019独角兽企业重金招聘Python工程师标准>>>
上一篇 https://my.oschina.net/flyrobin/blog/1819314 介绍了基于ext_skel构建的php扩展,当时只是体验一下基本的开发流程。现在我们进一步了解扩展的开发,先从定义自己的扩展的函数开始。
1. 背景知识
php函数分为两种:一是php代码中的用户函数(zend_function.op_array结构存储),另一种是扩展内的函数(zend_function.internal_function),二者都会注册到EG(function_table)中,php在执行时会根据函数名在function_table中查找。
2. 定义函数
PHP_FUNCTION(myfunction)
{
printf("It's my first extnsion!")
}
3. 模块中注册函数
配置zend_function_entry
const zend_function_entry firstext_functions[] = {
PHP_FE(firstext, NULL)
PHP_FE_END //末尾必须加这个
};
注册到模块中
zend_module_entry firstext_module_entry = {
STANDARD_MODULE_HEADER,
"firstext",
firstext_functions, //注册函数列表到模块中
PHP_MINIT(firstext),
PHP_MSHUTDOWN(firstext),
PHP_RINIT(firstext), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(firstext), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(firstext),
PHP_FIRSTEXT_VERSION,
STANDARD_MODULE_PROPERTIES
};