Home >Backend Development >PHP Tutorial >A brief discussion on the development of PHP Extension - Basics Page 1/2_PHP Tutorial
Summary & Introduction
PHP is a language that is currently widely used. It can be seen in various large, medium and small websites from foreign Facebook and Twitter to domestic Taobao, Tencent, Baidu and all kinds of large, medium and small websites on the Internet. . It should be said that the success of PHP relies largely on its open extension API mechanism and rich extension components (PHP Extension). It is these extension components that allow PHP to operate from various database operations to XML, JSON, encryption, file processing, It is omnipotent in fields such as graphics processing and Socket. Sometimes developers may need to develop their own PHP extensions. The current extension mechanism of PHP5 is based on Zend API. Zend API provides rich interfaces and macro definitions, plus some practical tools, making it not particularly difficult to develop PHP extensions. . This article will introduce basic knowledge about the development of PHP extension components, and demonstrate the basic process of developing PHP extensions through an example.
The development process of PHP extension components is different in Unix and Windows environments, but they are basically interoperable. This article will be based on the Unix environment (specifically using Linux). Reading this article requires a simple understanding of some basic knowledge of the Unix environment, PHP and C language. As long as you have a simple understanding, I will try not to cover too specific operating system and language features, and explain them where necessary to facilitate readers' reading.
The specific development environment of this article is Ubuntu 10.04 + PHP 5.3.3.
Download PHP source code
To develop PHP extensions, the first step is to download the PHP source code, because it contains the tools needed to develop extensions. What I downloaded is the latest version of PHP 5.3.3, in the format of tar.bz2 compressed package. The download address is: http://cn.php.net/get/php-5.3.3.tar.bz2/from/a/mirror.
After downloading, move the source code to the appropriate directory and unzip it. The decompression command is:
At this time, use ls to view it, and you will find that there is an additional "say_hello" directory. Enter this directory, and you will find that ext_skel has established the basic framework of say_hello for us, as shown below:
If you are too lazy to figure out the entire contents of the PHP extension package directory structure, then there are three files in it that you must pay attention to:
config.m4: This is the Build System configuration file in the Unix environment, which will be used to generate configuration and installation later.
php_say_hello.h: This file is the header file of the extension module. Following the consistent style of C language, some custom structures, global variables, etc. can be placed in this.
say_hello.c: This is the main program file of the extension module. The final function entries of the extension module are here. Of course, you can stuff all the program code into it, or you can follow the modular idea and put each functional module into different files.
The following content mainly revolves around these three files.
The first step in developing PHP extension components is not to write the implementation code, but to configure the Build System option first. Since we are developing under Linux, the configuration here is mainly related to config.m4.
Regarding the Build System configuration, if I can write a lot about it, and it is related to many things in the Unix system, even if I am interested in writing it, I guess no one will be interested in reading it, so we will omit it here and only focus on the key points. For more details about config.m4, please refer here.
Open the generated config.m4 file, the content is roughly as follows:
这个结构体可能看起来会让人有点头疼,不过我还是要解释一下里面的内容。因为这就是PHP Extension的原型,如果不搞清楚,就没法开发PHP Extension了。当然,我就不一一对每个字段进行解释了,只拣关键的、这篇文章会用到的字段说,因为许多字段并不需要我们手工填写,而是可以使用某些预定义的宏填充。
第7个字段“name”,这个字段是此PHP Extension的名字,在本例中就是“say_hello”。
第8个字段“functions”,这个将存放我们在此扩展中定义的函数的引用,具体结构不再分析,有兴趣的朋友可以阅读_zend_function_entry的源代码。具体编写代码时这里会有相应的宏。
第9-12个字段分别是四个函数指针,这四个函数会在相应时机被调用,分别是“扩展模块加载时”、“扩展模块卸载时”、“每个请求开始时”和“每个请求结束时”。这四个函数可以看成是一种拦截机制,主要用于相应时机的资源分配、释放等相关操作。
第13个字段“info_func”也是一个函数指针,这个指针指向的函数会在执行phpinfo()时被调用,用于显示自定义模块信息。
第14个字段“version”是模块的版本。
(关于zend_module_entry更详尽的介绍请参考这里)
介绍完以上字段,我们可以看看“say_hello.c”中自动生成的“say_hello_module_entry”框架代码了。
First of all, the macro "STANDARD_MODULE_HEADER" will generate the first 6 fields, and "STANDARD_MODULE_PROPERTIES" will generate the fields after "version", so we don't have to worry about it yet. The several fields we care about have also been filled in or generated by macros, and the frameworks of several functions have also been generated at the corresponding locations in "say_hello.c". It should be noted here that the parameters of several macros are all "say_hello", but this does not mean that the names of several functions are all "say_hello", and there is no function name overloading mechanism in C language. In fact, in the process of developing PHP Extension, various macros predefined in Zend are used almost everywhere, from global variables to function definitions and even return values. C language cannot be written in a "naked" way. , this is because the operating mechanism of PHP may cause problems such as naming conflicts, and these macros will transform elements such as functions into an internal name, but these are transparent to programmers (unless you read the code of those macros), We program through various macros, and macros handle a lot of internal stuff for us.
After writing this, our task is clear: first, if we need to process something at the corresponding time, we need to fill in the contents of each interception function; second, write the function function of say_hello and add the reference to say_hello_functions .
Because the say_hello extension does not require operations at each life cycle stage, we only write the content of info_func. As mentioned above, this function will be automatically called when phpinfo() is executed to display extension information. Four functions will be used to write this function:
php_info_print_table_start()——Start phpinfo table. No parameters.
php_info_print_table_header()——Output table header. The first parameter is an integer, indicating the number of columns in the header, and the subsequent parameters are (char*) type parameters equal to the number of columns used to specify the text to be displayed.
php_info_print_table_row()——Output table content. The first parameter is an integer, indicating the number of columns in this row, and the subsequent parameters are (char*) type parameters equal to the number of columns used to specify the text to be displayed.
php_info_print_table_end()——End the phpinfo table. No parameters.