PHP7 Kernel Analysis 11 Module Extension
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
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_参数名:获取对应的参数值
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. 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结构体
d. Generate configure: After the extension is written, execute the phpize script to generate configure and other configuration files ;
phpsize
e. Compile & install: ./configure, make, make install, and then add the extended .so path to php.ini.
./configure make make install
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
