Home > Article > Backend Development > Write your own PHP extension function (1)_PHP tutorial
Write your own PHP extension function Yorgo Sun 2002/01/22 I have been writing PHP programs for a long time, so I naturally know the functions it provides very well. The many functions it provides are really useful, but sometimes I find that PHP is also Lacking some functions, I always have the idea of adding some custom functions to php. As time went by, I finally couldn't hold it any longer today and started researching how to add it. Download a PHP source code package. The version used here is PHP 4.0.5. After decompression, you will see a file like README.EXT_SKEL in the root directory of PHP. Open it and read it in detail, and you will find a very useful one. Tool, this tool can help you build an empty php extension, and then you can add the corresponding code to it to complete your own function extension. Below we will introduce how to use this tool. First transfer your directory to the ext directory under the php directory. If you only need a basic extension framework, execute the following command: ./ext_skel --extname=module_namemodule_name is the name of the extension module you can choose, for example I selected my_module. After executing the tool, a directory with the module_name of your choice will be automatically created in the ext directory. Relevant codes have been generated in it. In these codes, you only need to adjust the three lines of comments in the config.m4 file to compile normally with this customization. Extension module for PHP. You can get it by performing the following operations in the root directory of php. ./buildconf./configure --enable-module_namemake Next I will demonstrate the whole process of establishing the my_module extension framework. In order to be more effective, we will complete a PHP extension function. Calling this function in PHP can display hello on the web page. The classic word world. In the ext directory under the php directory, execute the following command./ext_skel --extname=my_module to get the feedback results: Creating directory my_moduleCreating basic files: config.m4 Makefile.in .cvsignore my_module.c php_my_module.h tests/001.phpt my_module.php [done]. To use your new extension, you will have to execute the following steps:1. $ cd ..2. $ vi ext/my_module/config.m43. $ ./buildconf4. $ ./configure - -[with|enable]-my_module5. $ make6. $ ./php -f ext/my_module/my_module.php7. $ vi ext/my_module/my_module.c8. $ make Repeat steps 3-6 until you are satisfied with ext/ my_module/config.m4 andstep 6 confirms that your module is compiled into PHP. Then, start writingcode and repeat the last two steps as often as necessary. If you can understand the above, then follow it. If you don’t understand it very well, you can just follow my tips below. Cd my_module First enter the my_module directory vi config.m4 Use a text editor to open the config.m4 file. The file content is roughly as follows: dnl $Id$dnl config.m4 for extension my_modulednl dont forget to call PHP_EXTENSION(my_module) dnl Comments in this file start with the string dnl.dnl Remove where necessary. This file will not workdnl without editing. dnl If your extension references something external, use with: dnl PHP_ARG_WITH(my_module, for my_module support,dnl Make sure that the comment is aligned:dnl [ - -with-my_module Include my_module support]) dnl Otherwise use enable: dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,dnl Make sure that the comment is aligned:dnl [ --enable-my_module Enable my_module support]) if test "$ PHP_MY_MODULE" != "no"; then dnl If you will not be testing anything external, like existence of dnl headers, libraries or functions in them, just uncomment the dnl following line and you are ready to go. dnl Write more examples of tests here... PHP_EXTENSION(my_module, $ext_shared)Fi Modify dnl PHP_ARG_WITH(my_module, for my_module support,dnl Make sure that the comment is aligned:dnl [ --with-my_module Include my_module support]) according to your own choice PHP_ARG_WITH(my_module, for my_module support,Make sure that the comment is aligned:[ --with-my_module Include my_module support]) or dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,dnl Make sure that the comment is aligned:dnl [ --enable-my_module Enable my_module support]) is changed to PHP_ARG_ENABLE(my_module, whether to enable my_module support,Make sure that the comment is aligned:[ --enable-my_module Enable my_module support]) Generally I will choose the latter, and then Save and exit. If you have difficulty operating the vi text editor, please refer to the corresponding instruction article, which will not be described in detail here.Vi my_module.c modify the following code in the file /* Every user visible function must have an entry in my_module_functions[].*/function_entry my_module_functions[] = { PHP_FE(say_hello, NULL) /* ß adds a line of code*/ PHP_FE(confirm_my_module_compiled, NULL) /* For testing, remove later. */ {NULL, NULL, NULL} /* Must be the last line in my_module_functions[] */}; Add the following code at the end of the file PHP_FUNCTION(say_hello){ zend_printf("hello world ");} Save the file and exit vi php_my_module.h. In the file PHP_FUNCTION(confirm_my_module_compiled); add the following code PHP_FUNCTION(say_hello) before the line; save the file and exit. Return to the root directory of php and execute the following command./buildconf./ configure --enable-my_modulemake If everything goes well, we have now compiled the extension module my_module into php. We write the following code for testing Say_hello();?> Save the file as say_hello.php and run it in the root directory of php./php –q say_hello.php Normally, hello world will be displayed, indicating that our first extension is running normally! Explain the above operation, ext_skel generates some files under the box, we need to modify the following file my_module.c The main extension module Program php_my_module.h The header file config.m4 of the extension module. The main program of the configuration file describes the declaration of the php extension module, how many functions the module contains, the role of each function, what is displayed in the phpinfo function, and what is done during module initialization. , everything done at the end will be described in this file. We just added a function say_hello above, and described the specific content of the say_hello function, calling the zend_printf system function to print the string in the corresponding header file. The say_hello function is used to complete the function we expected. Next, we will write a more complex extension and create a php extension function with parameters. According to the parameters given, hello world, xxxx represents the input string. Content, for example, my name is yorgo. Modify the final say_hello function content in Vi my_module.c as follows: PHP_FUNCTION(say_hello){ zval **yourname; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &yourname) == FAILURE) { WRONG_PARAM_COUNT; } zend_printf("hello world, %s ", Z_STRVAL_PP(yourname));} Save and exit. Return to the root directory of php, run make to modify say_hello.php to Say_hello(“yorgo”);?>, save and exit, and run./php –q say_hello.php to get the result hello world, yorgo means we are here The modification was also successful. You can change the parameters in say_hello to see the dynamic effect. Here we mainly explain the function content modified above. Since the say_hello function needs to have parameters introduced, the say_hello function in my_module.c mainly performs parameters. Processing, the parameter content filled in when referencing say_hello in php is correctly passed to the say_hello processing function in my_module.c. For this purpose, these lines are added to the program zval **yourname;if (ZEND_NUM_ARGS()! = 1 || zend_get_parameters_ex(1, &yourname) == FAILURE){WRONG_PARAM_COUNT;}zend_printf("hello world, %s ", Z_STRVAL_PP(yourname)); The code explanation is as follows: zval **yourname; Initialize a parameter pointer ZEND_NUM_ARGS() to get the number of parameters passed, and judge that if it is not 1, it means there is a problem and report an error. zend_get_parameters_ex(1, &yourname) points the just initialized pointer to the passed parameter, and reports an error if it fails. Z_STRVAL_PP(yourname) processes the parameter pointed to by the pointer and obtains the actual stored value.