Home > Backend Development > PHP Tutorial > PHP7 Kernel Analysis 11 Module Extension

PHP7 Kernel Analysis 11 Module Extension

不言
Release: 2023-03-23 18:16:01
Original
1179 people have browsed it

The content of this article is about the module extension of PHP7 kernel analysis 11. Now I share it with you. Friends in need can refer to it

1. Compilation tools

(a).ext_skel: This script mainly generates the configuration required for compilation and the basic structure of the extension

(b).php-config: This script mainly obtains PHP installation information

(c).phpize: used to generate configure files


2. Basic steps for writing extensions

a. Generate the basic framework of the extension through the ext_skel script in the ext directory;
./ext_skel --extname=wu
Copy after login
b. Modify the config.m4 configuration: set the compilation configuration parameters, set the extended source file, dependency library/function check, etc.;
PHP_ARG_WITH(arg_name,check message,help info): 定义一个--with-feature[=arg]这样的编译参数,参数分别为
参数名、执行./configure是展示信息、执行--help时展示信息

$PHP_参数名:获取对应的参数值
Copy after login
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。
Copy after login
./configure时输出结果,其中error将会中断configure执行

AC_MSG_CHECKING(message)
AC_MSG_RESULT(message)
AC_MSG_ERROR(message)
Copy after login
AC_DEFINE(variable, value, [description]): 定义一个宏,比如:AC_DEFINE(IS_DEBUG, 1, []),执行autoheader
时将在头文件中(config.h)生成:#define IS_DEBUG 1。
Copy after login
PHP_ADD_INCLUDE(path): 添加include路径,即:gcc -Iinclude_dir
Copy after login
PHP_CHECK_LIBRARY(library, function [, action-found [, action-not-found ]]): 检查依赖的库中是否存在需要
的function,action-found为存在时执行的动作,action-not-found为不存在时执行的动作
Copy after login
PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $XXX_DIR/$PHP_LIBDIR, XXX_SHARED_LIBADD): 添加链接库
Copy after login
PHP_NEW_EXTENSION(extname, sources [, shared]): 注册一个扩展,添加扩展源文件,确定此扩展是动态库还是静态库,每个扩展的config.m4中都需要通过这个宏完成扩展的编译配置。
Copy after login
c. Write the function to be implemented by the extension: write the function according to the format of the PHP extension and the API provided by PHP;
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结构体
Copy after login
d. Generate configure: After the extension is written, execute the phpize script to generate configure and other configuration files ;
phpsize
Copy after login
e. Compile & install: ./configure, make, make install, and then add the extended .so path to php.ini.
./configure
make
make install
Copy after login

Related recommendations:

PHP7 Kernel Analysis 10: Thread Safety

PHP7 Kernel Analysis 9: Memory Management

PHP7 Kernel Analysis 8 and the like


##

The above is the detailed content of PHP7 Kernel Analysis 11 Module Extension. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template