Write your own PHP extension function (a) _php tutorial

Source: Internet
Author: User
Tags php source code
Write your own php extension function Yorgo Sun 2002/01/22 PHP program has been written for a long time, naturally, he provides a good understanding of the function, he provides a lot of functions, really feel very useful, but sometimes found that PHP also lacks some features, You will always have the idea of adding some custom features to PHP. Over time, finally today, and began to study how to add. Download a PHP source code package, the use of PHP 4.0.5 version, the decompression will see the root directory of PHP will have readme.ext_skel such a file, open the detailed reading, found a very useful tool, this tool can help you build an empty PHP extension, Then you can add the appropriate code to the inside to complete your own extension of the function. Let's show you how to use this tool. First move your directory to the EXT directory in 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 chose the my_module. Executing the tool will automatically create your chosen module_name name in the EXT directory, which has generated the relevant code, the code only need to adjust the Config.m4 file three lines of comments can be normal to compile with this custom extension PHP. The following actions can be obtained from the root directory of PHP:/buildconf./configure--enable-module_namemake I'll show you the whole process of building the My_module extension framework, in order to be more effective, Let's do a PHP extension and call this feature in PHP to display the classic Hello World word in a 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 and you'll 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 be satisfied with EXT/MY_MODULE/CONFIG.M4 Andstep 6 confirms this your module is Compil Ed into PHP. Then, start Writingcode and repeat the last of the steps as often as necessary. If you can read the above, do it. If you are not too clear, follow my tips below to do it. Cd My_module first into the My_module directory VI config.m4 use a text editor to open the Config.m4 file, the contents of the file are 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 Necessar Y. This file is 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 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 are ALIGNED:DNL [--enable-my_module enable my_module support]) if test "$PHP _my_module"! = "no"; Then DNL If you are not being testing anything external, like existence of DNL headers, libraries or functions in them, just Uncomment the DNL following line and your is ready to go. DNL Write More examples of tests ... Php_extension (My_module, $ext _shared) Fi will dnl Php_arg_with (My_module, for My_module support,dnl make sure) according to your own choice The comment is aligned:dnl [--with-my_module Include my_module support]) modified to Php_arg_with (My_module, for My_module , make sure this comment is aligned:[--with-my_module Include my_module support]) or will DNL php_arg_enable (My_module, whet She to enable My_module support,dnl Make sure, the comment is ALIGNED:DNL [--enable-my_module enable My_module Suppor T]) modified into php_arg_enable (My_module, whether to ENABLE My_module Support,make sure that the comment is aligned:[--enable-my_mo Dule Enable My_module support]) generally I will choose the latter and then save the exit. If youIf you have difficulty with the operation of the vi text editor, please refer to the corresponding description article, which is not described in detail here. Vi my_module.c Modify the following code in the file */* Every user visible function must has a 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, RE Move later. */{NULL, NULL, NULL}/* must is the last line in my_module_functions[] */}; At the end of the file, add the following code php_function (Say_hello) {zend_printf ("Hello World");} Save file Exit vi php_my_module.h in file php_function (confirm_my_module_compiled); Add the following code php_function (Say_hello) before a line; Save file exit Go back to the root directory of PHP and execute the following command./buildconf./configure--enable-my_modulemake If all goes well, we have now compiled the extension my_module into PHP. We write the following code to test Save the file as say_hello.php run in the root directory of PHP./php–q say_hello.php Normally shows Hello world, which means our first extension runs properly! To explain the above actions, Ext_skel generate some boxes under the file, we need to modify the following file my_module.c Extension module of the main program php_my_module.h Extension Module header file config.m4 configuration file main program describes the PHP extension module declaration , how many functions are in the module, what each function does, what is displayed in the Phpinfo function, what the module initializes, and what is done in this file will be described in this document. We just added a function Say_hello above and described the specifics of the Say_hello function, calling the zend_printf system function to print the string in PHP. The function of Say_hello is declared in the corresponding header file, which completes the function we expect. Below we will write a more complex extension, create a php extension function with parameters, according to the parameters given, show Hello World, xxxx. XXXX represents the input string content, such as my name Yorgo. Vi my_module.c Modify the contents of the last Say_hello function 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 to exit. Return to the root directory of PHP and run make to modify say_hello.php to Save to run after exiting./php–q say_hello.php The results Hello World, Yorgo said that our changes have also been successful, can change the parameters in Say_hello, see the dynamic effect. Here mainly explains the above modified function content, because the Say_hello function needs to have the parameter introduction, therefore in the my_module.c the Say_hello function mainly carries on the parameter processing, the PHP reference Say_hello when fills in the parameter content to pass correctly to My_ Module.c in the Say_hello handler function. To do this, a few 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 is interpreted as follows: Zval **yourname; Initializes a pointer to a parameter Zend_num_args () gets the number of arguments passed over, and determines if a problem is indicated if it is not 1, an error. ZEND_GET_PARAMETERS_EX (1, &yourname) points the newly initialized pointer to the passed argument and, if unsuccessful, an error. Z_STRVAL_PP (yourname) handles the parameters pointed to by the pointer and obtains the actual stored value.

(to be continued) The online reprint, but please keep the author's copyright notice, if you want to publish offline, please contact the author Yorgo@163.net http://www.ruisoft.com

http://www.bkjia.com/PHPjc/532359.html www.bkjia.com true http://www.bkjia.com/PHPjc/532359.html techarticle Write your own php extension function Yorgo Sun 2002/01/22 PHP program written for a long time, naturally, he provides a good understanding of the function, he provides a lot of functions, really feel very useful, ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.