Write your own PHP extension function PHP program written for a long time, naturally, he is very knowledgeable about the functions he offers, and he offers a lot of features that really feel good, but sometimes it turns out that PHP lacks some functionality and always produces ideas for adding some custom functionality to PHP. Over time, finally today can not hold up, began to study how to add.
Download a PHP source code package, here is the use of PHP 4.0.5 version, decompression will see the root directory of PHP will have readme.ext_skel such a file, open a detailed reading, found a very useful tool, this tool can help you build an empty PHP extension, Then you add the corresponding code to the inside to complete your own extension of the function. Here's how to use this tool.
First transfer your directory to the PHP directory under the EXT 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 from, such as the my_module I chose. After executing the tool will automatically in the EXT directory to create your choice of module_name name directory, which has generated the relevant code, which only need to adjust the Config.m4 file in the three lines of comments can normally compile with this custom extension module PHP. You can get it by doing the following in the root directory of PHP.
./buildconf
./configure--enable-module_name
Make
Let me show you the whole process of building the My_module extension framework, and in order to be more effective, we're going to do a PHP extension that calls this feature in PHP to display the word Hello World in a Web page.
In the Ext directory under the PHP directory, execute the following command
Php_arg_enable (My_module, whether to ENABLE my_module support,
Make sure this 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 in the operation of the vi text editor, please refer to the corresponding explanatory article, this is no longer described in detail.
Vi my_module.c
Modify the following code in the file
/* Every user visible function must have 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 a file, add the following code before a line
Php_function (Say_hello);
Save File Exit
Return 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
Running in the root directory of PHP
./php–q say_hello.php
is normally displayed
Hello World
It means our first extension is working!
Explain the operation above, Ext_skel generate some boxes of files, we need to modify the following file
MY_MODULE.C Extension Module Main program
Php_my_module.h the header file of the extension module
CONFIG.M4 configuration file
The main program describes the statement of the PHP extension module, the module contains a number of functions, the functions of each function, what to display in the Phpinfo function, module initialization to do something, the end of what will be described in this file. We just added a function Say_hello, and described the specific contents of the Say_hello function, calling zend_printf system function to print a string in PHP.
The Say_hello function is declared in the corresponding header file, which completes the function we expect. Below we'll write a more complex extension that creates a php extension function with parameters, according to the parameters given, to show Hello World, xxxx. XXXX represents the string content of the input, such as my name Yorgo.
Vi my_module.c
Modify 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)
We have also succeeded in changing the parameters in Say_hello to see the dynamic effect.
This mainly explains the modified function content, because the Say_hello function needs to have the parameter introduction, therefore in MY_MODULE.C's Say_hello function mainly in carries on the parameter processing, the PHP reference Say_hello when fills in the parameter content correctly passes to the My_ In the Say_hello handler function in module.c. To do this, you add a few lines to your program.
Zval **yourname;
if (Zend_num_args ()!= 1
ZEND_GET_PARAMETERS_EX (1, &yourname) = = failure)
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.