Automatic generation of extension framework for php extension
Preface
Previous article: Novice learns PHP extension: hello world. Regardless of the reason, he forcibly says hello with PHP extension. The framework automatically generated by ext_skel will be explained in detail in this article as a memo.
Text
Usage of ext_skel
./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]] [--skel=dir] [--full-xml] [--no-help] --extname=module module is the name of your extension(模块名,会在当前目录创建一个该名称子目录) --proto=file file contains prototypes of functions to create(函数原型定义文件) --stubs=file generate only function stubs in file --xml generate xml documentation to be added to phpdoc-cvs --skel=dir path to the skeleton directory(设置骨架生成的目录,不设置该项则默认在ext/extname下) --full-xml generate xml documentation for a self-contained extension (not yet implemented) --no-help don't try to be nice and create comments in the code and helper functions to test if the module compiled (生成的代码中不显示各种帮助注释)
php and extension-related processes
1. The startup and termination of the PHP program are conceptually separate. of.
One is when the php module is loaded, the module startup function is called by the engine (PHP_MINIT_FUNCTION). This causes the engine to do some initialization such as resource types, registering INI variables, etc., and these data are resident in memory, corresponding to a termination (PHP_MSHUTDOWN_FUNCTION)
The other is when the PHP request starts, the request Don't call the previous startup function (PHP_RINIT_FUNCTION), which corresponds to the termination after the request is completed (PHP_RSHUTDOWN_FUNCTION)
2. As PHP starts, it will start to process the MINIT method of all its loaded extensions. (The full name is Module Initialization, which is a function defined by each module.) (PHP_MINIT_FUNCTION), execute it once. During this time, the extension can define some of its own constants, classes, resources, etc., all of which will be used by the user-side PHP script. s things. The stuff defined here will be resident in memory and can be used by all requests until the PHP module is turned off.
3. When a request comes, PHP will quickly open up a new environment, rescan its own extensions, and traverse and execute their respective RINIT methods (full name Request Initialization) (PHP_RINIT_FUNCTION). At this time, a The extension may initialize variables that will be used in this request, etc., and may also initialize variables in the client (i.e. PHP script) later, etc.
4. When the request passes through the business code and is executed to the end, PHP will start the recycling program and execute the RSHUTDOWN (full name Request Shutdown) (PHP_RSHUTDOWN_FUNCTION) method of all loaded extensions, using variables in the kernel Once the execution is completed, everything used in this request will be released, including all variables in the variable table, all memory requested in this request, etc.
5 After the request processing is completed, the ones that should be closed are also closed, and PHP will enter the MSHUTDOWN (full name Module Shutdown) (PHP_MSHUTDOWN_FUNCTION) stage. At this time, PHP will issue an ultimatum to all extensions. If any extension still has unfulfilled wishes, let it go. In its own MSHUTDOWN method, this is the last chance. Once PHP finishes executing the extended MSHUTDOWN, it will enter the self-destruct program. (The last chance to clear the memory applied for without authorization, otherwise the memory will leak)
Summary, the process I understand:
PHP_MINIT_FUNCTION (executed once per process)
|
Execute many PHP_RINIT_FUNCTION
|
Execute many PHP_RSHUTDOWN_FUNCTION
|
PHP_MSHUTDOWN_FUNCTION (executed once per process)
Attached are the diagrams of multi-threading and multi-process
dnl $Id$ dnl config.m4 for extension helloworld dnl Comments in this file start with the string 'dnl'. dnl Remove where necessary. This file will not work dnl without editing. dnl If your extension references something external, use with:##指定PHP模块的工作方式,动态编译选项,如果想通过.so的方式接入扩展,请去掉前面的dnl注释PHP_ARG_WITH(helloworld, for helloworld support, Make sure that the comment is aligned: [ --with-helloworld Include helloworld support]) dnl Otherwise use enable:##指定PHP模块的工作方式,静态编译选项,如果想通过enable的方式来启用,去掉dnl注释PHP_ARG_ENABLE(helloworld, whether to enable helloworld support, Make sure that the comment is aligned: [ --enable-helloworld Enable helloworld support])if test "$PHP_HELLOWORLD" != "no"; then dnl Write more examples of tests here... dnl # --with-helloworld -> check with-path dnl SEARCH_PATH="/usr/local /usr" # you might want to change this dnl SEARCH_FOR="/include/helloworld.h" # you most likely want to change this dnl if test -r $PHP_HELLOWORLD/$SEARCH_FOR; then # path given as parameter dnl HELLOWORLD_DIR=$PHP_HELLOWORLD dnl else # search default path list dnl AC_MSG_CHECKING([for helloworld files in default path]) dnl for i in $SEARCH_PATH ; do dnl if test -r $i/$SEARCH_FOR; then dnl HELLOWORLD_DIR=$i dnl AC_MSG_RESULT(found in $i) dnl fi dnl done dnl fi dnl dnl if test -z "$HELLOWORLD_DIR"; then dnl AC_MSG_RESULT([not found]) dnl AC_MSG_ERROR([Please reinstall the helloworld distribution]) dnl fi dnl # --with-helloworld -> add include path dnl PHP_ADD_INCLUDE($HELLOWORLD_DIR/include) dnl # --with-helloworld -> check for lib and symbol presence dnl LIBNAME=helloworld # you may want to change this dnl LIBSYMBOL=helloworld # you most likely want to change this dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL, dnl [ dnl PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $HELLOWORLD_DIR/$PHP_LIBDIR, HELLOWORLD_SHARED_LIBADD) dnl AC_DEFINE(HAVE_HELLOWORLDLIB,1,[ ]) dnl ],[ dnl AC_MSG_ERROR([wrong helloworld lib version or lib not found]) dnl ],[ dnl -L$HELLOWORLD_DIR/$PHP_LIBDIR -lm dnl ]) dnl ##用于说明这个扩展编译成动态链接库的形式 dnl PHP_SUBST(HELLOWORLD_SHARED_LIBADD) ##用于指定有哪些源文件应该被编译,文件和文件之间用空格隔开 PHP_NEW_EXTENSION(helloworld, helloworld.c, $ext_shared)fi
#define PHP_HELLOWORLD_VERSION "0.1.0"
#ifdef HAVE_CONFIG_H #include "config.h" #endif ##包含头文件(引入所需要的宏、API定义等) #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_helloworld.h" static int le_helloworld; ##PHP核心定义的一个宏,与ZEND_FUNCTION相同,用于定义扩展函数(这个函数是系统默认生成的,用于确认之用) PHP_FUNCTION(confirm_helloworld_compiled) { char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "helloworld", arg); RETURN_STRINGL(strg, len, 0); } ##定义PHP中可以调用的函数 PHP_FUNCTION(helloworld) { php_printf("Hello World!\n"); RETURN_TRUE; } ##初始化module时运行 PHP_MINIT_FUNCTION(helloworld) { /* If you have INI entries, uncomment these lines REGISTER_INI_ENTRIES(); */ return SUCCESS; } ##当module被卸载时运行 PHP_MSHUTDOWN_FUNCTION(helloworld) { /* uncomment this line if you have INI entries UNREGISTER_INI_ENTRIES(); */ return SUCCESS; } ##当一个REQUEST请求初始化时运行 PHP_RINIT_FUNCTION(helloworld) { return SUCCESS; } ##当一个REQUEST请求结束时运行 PHP_RSHUTDOWN_FUNCTION(helloworld) { return SUCCESS; } ##声明模块信息函数,即可以在phpinfo看到的信息 PHP_MINFO_FUNCTION(helloworld) { php_info_print_table_start(); php_info_print_table_header(2, "helloworld support", "enabled"); php_info_print_table_end(); /* Remove comments if you have entries in php.ini DISPLAY_INI_ENTRIES(); */ } ##声明(引入)Zend(PHP)函数块 const zend_function_entry helloworld_functions[] = { PHP_FE(confirm_helloworld_compiled, NULL) /* For testing, remove later. */ ##上一讲中就是在这里添加了自己定义的函数模块 PHP_FE(helloworld, NULL) /* */ ##zend引擎认为结束的标记,老版本的是“{NULL,NULL,NULL}”,后面PHP源代码直接定义了个宏PHP_FE_END,这里就直接用这个了。虽然都一个意思但看过去爽多了 ##如果遇到PHP_FE_END未定义undefine的问题,请见附录1 PHP_FE_END /* Must be the last line in helloworld_functions[] */ }; ##声明 Zend模块,是不是感觉下面的模块名字很熟悉,对的,就是前文讲到的PHP流程中会用到的,现在懂了为什么要先讲流程了吧~ zend_module_entry helloworld_module_entry = { STANDARD_MODULE_HEADER, "helloworld", helloworld_functions, PHP_MINIT(helloworld), PHP_MSHUTDOWN(helloworld), PHP_RINIT(helloworld), /* Replace with NULL if there's nothing to do at request start */ PHP_RSHUTDOWN(helloworld), /* Replace with NULL if there's nothing to do at request end */ PHP_MINFO(helloworld), PHP_HELLOWORLD_VERSION, STANDARD_MODULE_PROPERTIES }; ##实现get_module()函数 #ifdef COMPILE_DL_HELLOWORLD ZEND_GET_MODULE(helloworld) #endif
PHP_FUNCTION is the same as this. The macro has been defined in /main/php.h
#define PHP_FUNCTION ZEND_FUNCTION
这个函数只用于动态可加载模块
6.实现导出函数。
实现想要扩展的函数,PHP_FUNCTION(helloworld)
ps:模块部分是学习这篇文章的,本来写好了,后面发现他写的比我好就借鉴了PHP扩展代码结构详解
附录
1.error: ‘PHP_FE_END’ undeclared here (not in a function)错误。
原因:是因为zend引擎版本太老了。
1、切换到php的源码目录,
2、执行下面两行
# sed -i 's|PHP_FE_END|{NULL,NULL,NULL}|' ./ext/**/*.c # sed -i 's|ZEND_MOD_END|{NULL,NULL,NULL}|' ./ext/**/*.c
3.切换到mcrypt目录,如php-5.x.x/ext/mcrypt/。再次执行make命令,一切恢复正常。
The above is the detailed content of Automatic generation of extension framework for php 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











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

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.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

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
