- Obtain PHP source code
Http://www.php.net/downloads.php#v5
- Decompress the source code package
- $ CD php-5.2.5/EXT
Create an extension function prototype file.
Gedit Tsing. proto
Input function prototype
String say_hello (string str_name)
Save and exit gedit
- Generate Extension
$./Ext_skel -- extname = Tsing -- proto = Tsing. proto
As the name suggests, ext_sket is the basic skeleton for generating the extension module. Tsing is the name of the extension module. After execution, a directory with the corresponding name will be created under the ext directory.
- $ CD Tsing
- Modify the config. M4 File
# Gedit./EXT/my_so_name/config. M4
Remove the following line of code comment mark "DNL"
Php_arg_with (Tsing, for tsing support,
Make sure that the comment is aligned:
[-- With-tsingInclude Tsing support])
Or the annotation mark of the following lines
Php_arg_enable (Tsing, whether to enable test support,
Make sure that the comment is aligned:
[-- Enable-tsingEnable test support])
These lines describe how PHP loads the so module during compilation with-tsing or enable-Tsing.
With-tsing indicates that a third-party library is required, and enable-tsing indicates that a third-party library is not required.
- # Gedit./EXT/Tsing. c
In row 43rd, The say_hello function is automatically registered in the header.
/* {Tsing_functions []
*
* Every user visible function must have an entry in tsing_functions [].
*/
Zend_function_entry tsing_functions [] = {
Php_fe (confirm_tsing_compiled, null)/* for testing, remove later .*/
Php_fe (say_hello, null)
{Null, null, null}/* must be the last line in tsing_functions [] */
};
/*}}}*/
In row 177th, we can see that the function defined in the prototype file Tsing. proto has generated the code skel in Tsing. C.
/* {Proto string say_hello (string str_name)
*/
Php_function (say_hello)
{
Char * str_name = NULL;
Int argc = zend_num_args ();
Int str_name_len;
If (zend_parse_parameters (argc tsrmls_cc, "S", & str_name, & str_name_len) = failure)
Return;
Php_error (e_warning, "say_hello: not yet implemented ");
}
/*}}}*/
Comment out 186th lines of code as follows
// Php_error (e_warning, "say_hello: not yet implemented ");
Add
Php_printf ("hello,". str_name .";");
Save and exit gedit
- View header files
# Gedit./EXT/Tsing/php_tsing.h
We found that the prototype of the say_hello function has been added in row 44.
Php_function (say_hello );
Save and exit gedit
- Configuration
There are two ways to configure
Method
#./Buildconf -- force (the force parameter is used to prevent your PHP version from being release)
# ../Configure -- disable-all -- With-tsing = shared -- with-apxs2 =/usr/sbin/apxs -- prefix =/usr/lib/PHP/modules
In the preceding command, -- disable-all is used to speed up compilation and reduce the number of modules to be compiled by default by PHP. -- With-tsing = shared to directly produce the. So file after compilation, -- with-apxs2 =/usr/sbin/apxs is determined based on the path and version of Apache installation on your serverMethod B
Find the bin/phpize in the PHP installation directory.
$/Usr/bin/phpize
Generate the configure file in the Tsing directory
Determine the installation location of the PHP-config file and apxs, and run configure
. /Configure -- With-PHP-Config =/usr/bin/PHP-config -- enable-tsing = shared -- with-apxs2 =/usr/sbin/apxs -- prefix =/usr/lib/ PHP/modules
- Compile
# Make
- Install
# Add gedit/etc/PHP. d/Tsing. ini to extension = Tsing. So# Make install or # cp./EXT/Tsing/. libs/Tsing. So/usr/lib/PHP/EXT/
- Restart Apache
- View the results of the phpinfo () function and check whether the Tsing Column exists.
- In this way, you can directly use dynamic extended functions in the calling file.
Echo say_hello ("Tsing ");
Output
Hello, Tsing;
References
Http://hi.baidu.com/thinkinginlamp/blog/item/58a2d7ca6c67eb46f21fe79a.html
Http://hi.baidu.com/thinkinginlamp/blog/item/7c691f3030084099a9018e1b.html
Http://blog.csdn.net/taft/archive/2006/02/10/596291.aspx How to Write PHP extensions
Installation of http://php.chinaunix.net/manual/zh/install.pecl.php PECL extension Library