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
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:
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.
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.