本篇文章的内容介绍的是关于php7内核剖析11之模块扩展 ,现在分享给大家,有需要的朋友可以参考一下
1. 编译工具
(a).ext_skel:这个脚本主要生成了编译需要的配置以及扩展的基本结构
(b).php-config:这个脚本主要是获取php的安装信息
(c).phpize:用于生成configure文件
2.编写扩展的基本步骤
a.通过ext目录下ext_skel脚本生成扩展的基本框架;./ext_skel --extname=wu
b.修改config.m4配置:设置编译配置参数、设置扩展的源文件、依赖库/函数检查等等;php_arg_with(arg_name,check message,help info): 定义一个--with-feature[=arg]这样的编译参数,参数分别为
参数名、执行./configure是展示信息、执行--help时展示信息
$php_参数名:获取对应的参数值
php_arg_enable(arg_name,check message,help info): 定义一个--enable-feature[=arg]或--disable-feature参
数,--disable-feature等价于--enable-feature=no,这个宏与php_arg_with类似,通常情况下如果配置的参数需
要额外的arg值会使用php_arg_with,而如果不需要arg值,只用于开关配置则会使用php_arg_enable。
./configure时输出结果,其中error将会中断configure执行
ac_msg_checking(message)
ac_msg_result(message)
ac_msg_error(message)
ac_define(variable, value, [description]): 定义一个宏,比如:ac_define(is_debug, 1, []),执行autoheader
时将在头文件中(config.h)生成:#define is_debug 1。
php_add_include(path): 添加include路径,即:gcc -iinclude_dir
php_check_library(library, function [, action-found [, action-not-found ]]): 检查依赖的库中是否存在需要
的function,action-found为存在时执行的动作,action-not-found为不存在时执行的动作
php_add_library_with_path($libname, $xxx_dir/$php_libdir, xxx_shared_libadd): 添加链接库
php_new_extension(extname, sources [, shared]): 注册一个扩展,添加扩展源文件,确定此扩展是动态库还是静态库,每个扩展的config.m4中都需要通过这个宏完成扩展的编译配置。
c.编写扩展要实现的功能:按照php扩展的格式以及php提供的api编写功能;php_minit_function(mytest){
这个阶段可以进行内部类的注册,如果你的扩展提供
了类就可以在此函数中完成注册;除了类还可以在此
函数中注册扩展定义的常量
}
php_rinit_function(mytest){
如果你的扩展需要针对每一个请求进行处理则可以设
置这个函数,如:对请求进行filter
}
php_rshutdown_function(mytest){
此函数在请求结束时被调用
}
php_mshutdown_function(mytest){
模块关闭阶段回调的函数,与module_startup_func对应,
此阶段主要可以进行一些资源的清理
}
php_function(my_func_1){
自定义内部函数1
}
php_function(my_func_1){
自定义内部函数2(带参)
zval *arr;
//l当数据溢出不报错,s需要第四参数,
//l(l)整型,(b)布尔型,(d)浮点型,s(s)字符串型,a(a)数组型,o(o)对象型,r资源型,z任意类型
if(zend_parse_parameters(zend_num_args(), "la", &lval, &arr) == failure){
return_false;
}
}
const zend_function_entry mytest_functions[] = {
php_fe(my_func_1, null)
php_fe(my_func_2, null)
php_fe_end //末尾必须加这个
};
zend_module_entry mytest_module_entry = {
standard_module_header, //宏统一设置
"mytest", //模块名
mytest_functions, //自定义函数数组
php_minit(mytest), //扩展初始化回调函数
php_mshutdown(mytest), //扩展关闭时回调函数
php_rinit(mytest), //请求开始前回调函数
php_rshutdown(mytest), //请求结束时回调函数
null, //php_minfo(mytest),php_info展示的扩展信息处理函数
"1.0.0",
standard_module_properties //宏统一设置
};
zend_get_module(mytest) //读取mytest_module_entry结构体
d.生成configure:扩展编写完成后执行phpize脚本生成configure及其它配置文件;phpsize
e.编译&安装:./configure、make、make install,然后将扩展的.so路径添加到php.ini中。./configure
make
make install
相关推荐:
php7内核剖析10之线程安全
php7内核剖析9之内存管理
php7内核剖析8之 类
以上就是php7内核剖析11之模块扩展 的详细内容。