Compile your own php extension functions (1 ). I have been writing my own php extension function YorgoSun20020122php for a long time. Naturally, I am familiar with the functions provided by him. I really think it is very easy to use the many functions provided by him, writing your own php extension function Yorgo Sun 2002/01/22 the php program has been written for a long time. Naturally, I know all the functions provided by him. the many functions provided by him are really useful, however, sometimes you may find that php also lacks some features, and you will always have the idea of adding some custom features to php. Over time, I finally got stuck today and started to study how to add it. Download a php source code package. php 4.0.5 is used here. after decompression, you will see README under the php root directory. an EXT_SKEL file. after reading it in detail, I found a very useful tool that can help you build an empty php extension, then you can add the corresponding code to it to complete your function expansion. Next 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, my_module. After the tool is executed, the directory named module_name of your choice is automatically created under the ext directory, which generates the relevant code. in the code, you only need to adjust the config. the three lines of comments in the m4 file can be used to compile php with the custom extension module normally. Execute the following operations in the php root directory .. /Buildconf. /configure -- the enable-module_namemake below I will demonstrate the whole process of establishing the my_module extension framework, in order to have better results, we can complete a php extension function, you can call this function in php to display the classic word hello world on the web page. In the ext directory under the php Directory, execute the following command. /ext_skel -- extname = my_module get feedback: 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, follow the instructions. If not, follow the instructions below. Cd my_module first enters the my_module directory vi config. m4 uses the text editor to open 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, d Nl 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 according to your own choice will 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]) to PHP_ARG_WITH (my_module, for my_module support, Make sure that the comment is aligned: [-- with-my_module Include my_module support]) or to 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]) modified to PHP_ARG_ENABLE (my_module, whether to enable my_module support, make sure that the c Omment is aligned: [-- enable-my_module Enable my_module support]) In general I will select the latter and save and exit. If you have difficulties in operations on the vi text editor, please refer to the relevant instructions and I will not describe them 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)/* Add 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 PHP_FUNCTION (say_hello) at the end of the file) {zend_printf ("hello world");} save the text Exit vi php_my_module.h in the file PHP_FUNCTION (confirm_my_module_compiled); add the following code PHP_FUNCTION (say_hello) in front of a line; save the file and return it to the php root directory 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. Let's write the following code for testing. Save the file as say_hello.php and run./php-q say_hello.php in the root directory of php. normally, "hello world" is displayed, indicating that our first extension is running normally! To explain the above operations, ext_skel generates some files in the box. we need to modify the config header file of the main program php_my_module.h extension module of the my_module.c extension module. the main program of the m4 configuration file describes the declaration of the php extension module, the number of functions contained in the module, the role of each function, what is displayed in the phpinfo function, and what the module initializes, this file will describe what you want to end. We just added a function say_hello and described the specific content of the say_hello function. we called the zend_printf system function to print the string in php. In the corresponding header file, declare the say_hello function to complete our expected functions. Next we will compile a more complex extension, create a php extension function with parameters, and display hello world, xxxx According to the input parameters. Xxxx indicates the input string content, for example, my name is yorgo. Vi my_module.c the last modified say_hello function content is 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 the disk and exit. Return to the php root directory and run make to change say_hello.php After saving and exiting, run./php-q say_hello.php to get the result hello world. yorgo indicates that our modification was successful. you can change the parameters in say_hello to see the dynamic effect. Here we mainly explain the content of the modified function. because the say_hello function requires parameter introduction, the say_hello function in my_module.c is mainly processing parameters, pass the parameter content entered when say_hello is referenced in php to the say_hello handler function in my_module.c. To this end, the program adds these lines. 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 is interpreted as follows: zval ** yourname; initialize a parameter pointer ZEND_NUM_ARGS () to obtain the number of passed parameters. if it is not set to 1, it indicates that there is a problem and an error is returned. Zend_get_parameters_ex (1, & yourname) points the Initialized pointer to the passed parameter. if it fails, an error is returned. Z_STRVAL_PP (yourname) processes the parameters pointed to by the pointer and obtains the actually stored values.
(To be continued) Welcome to reprint on the Internet, but please retain the author's copyright statement, if you need to publish offline, please contact the author of yorgo@163.net http://www.ruisoft.com
Sun 2002/01/22 the php program has been written for a long time. Naturally, I am familiar with the functions provided by him. many of the functions provided by him are really useful ,...