[Turn to an older article] write your own PHP extension function _php

Source: Internet
Author: User
Keywords Extension function self article writing comparison
Tags php source code
Author: floating in the Quartet
I have not tested, interested can test under

Yorgo Sun 2002/01/22



The PHP program has been written for a long time, naturally, he has the ability to provide a lot of features, he has provided a large number of functions, it is very useful, but sometimes found that PHP is also missing some features, you will always have to add some custom functions for PHP ideas. 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_name

Module_name is the name of the extension module that you can choose, such as the my_module I chose. 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 operations are available at the root of the PHP directory.

./buildconf

./configure--enable-module_name

Make



Let me illustrate the whole process of building the My_module extension framework, in order to be more effective, we have to complete a php extension function, in PHP call this feature can display in the Web page Hello World the classic word.

In the Ext directory under the PHP directory, execute the following command

./ext_skel--extname=my_module

Get feedback on the results:

Creating Directory My_module

Creating 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.M4

3. $./buildconf

4. $./configure--[with|enable]-my_module

5. $ make

6. $./php-f ext/my_module/my_module.php

7. $ VI ext/my_module/my_module.c

8. $ make



Repeat steps 3-6 until you is satisfied with EXT/MY_MODULE/CONFIG.M4 and

Step 6 confirms that your module is compiled into PHP. Then, start writing

Code and repeat the last 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 enter the My_module directory

VI CONFIG.M4

Use a text editor to open the Config.m4 file with the following file contents:

DNL $Id $

DNL config.m4 for extension my_module

DNL don ' t forget to call Php_extension (My_module)



DNL Comments in this file start with the string ' DNL '.

DNL Remove where necessary. This file won't work

DNL 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, the comment is 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



Depending on your own choice, you will

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])

Modified into

Php_arg_with (My_module, for my_module support,

Make sure, the comment is aligned:

[--with-my_module Include my_module support])

or the

DNL php_arg_enable (My_module, whether to ENABLE my_module support,

DNL Make sure, the comment is aligned:

DNL [--enable-my_module enable my_module support])

Modified into

Php_arg_enable (My_module, whether to ENABLE my_module support,

Make sure, the comment is aligned:

[--enable-my_module enable my_module support])



Generally I will choose the latter and then save the exit. If you have difficulty with the VI text editor, please refer to the corresponding article, which is not described in detail here.

Vi my_module.c

Modify the following code in the file

/* Every user visible function must 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, remove later. */

{null, NULL, NULL}/* must is 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\n");

}

Save File Exit



VI php_my_module.h

Php_function (confirm_my_module_compiled) in the file, add the following code in front of the line

Php_function (Say_hello);

Save File Exit



Go back to the root directory of PHP and execute the following command

./buildconf

./configure--enable-my_module

Make



If all goes well, we have now compiled the extension module my_module into PHP. We write the following code to test


Say_hello ();

?>

Save File as say_hello.php

Run in the root directory of PHP

./php–q say_hello.php

is normally displayed

Hello World

Indicates that our first extension is running properly!



Explain the actions above, Ext_skel generate some boxes of files, we need to modify the following file

Main program of MY_MODULE.C extension module

Php_my_module.h header file for extension module

CONFIG.M4 configuration file



The main program describes the declaration of the PHP extension module, the number of functions in the module, the function of the functions, what is displayed in the Phpinfo function, what the module is initialized to do, and what will be described in this file. 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 last Say_hello function to read 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\n", Z_strval_pp (yourname));

}

Save to exit.

Return to the root directory of PHP and run

Make

Modify say_hello.php to


Say_hello ("Yorgo");

?>

Save to run after exiting

./php–q say_hello.php

Come to the results

Hello World, Yorgo

We have also succeeded in changing the parameters in the Say_hello to 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\n", Z_strval_pp (yourname));



The code is interpreted as follows:

Zval **yourname;

Initializes a pointer to a parameter

Zend_num_args ()

Get the number of parameters passed over, and judge if it is not 1 to indicate a problem, error.

ZEND_GET_PARAMETERS_EX (1, &yourname)

The pointer you just initialized points to the passed argument, and if unsuccessful, an error.

Z_STRVAL_PP (yourname)

Handles the parameter pointed to by the pointer and obtains the actual stored value.

adjourned
  • 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.