1. Build a php Environment
Download php 5.2.6 source code and decompress it
Compile and install the php Environment
2. Create an Extension Project
Go to the source code directory
Cd php5.2.6/ext/
./Ext_skel -- extname = my_ext
Create a project named my_ext and generate my_ext.so.
3. Change configurations and programs
$ Vi ext/my_ext/config. m4
Choose
Dnl PHP_ARG_WITH (my_ext, for my_ext support,
Dnl Make sure that the comment is aligned:
Dnl [-- with-my_ext Include my_ext support])
Modify
PHP_ARG_WITH (my_ext, for my_ext support,
Make sure that the comment is aligned:
[-- With-my_ext Include my_ext support])
Or set
Dnl PHP_ARG_ENABLE (my_ext, whether to enable my_ext support,
Dnl Make sure that the comment is aligned:
Dnl [-enable-my_ext Enable my_ext support])
Modify
PHP_ARG_ENABLE (my_ext, whether to enable my_ext support,
Make sure that the comment is aligned:
[-Enable-my_ext Enable my_ext support])
$ Vi ext/my_ext/php_my_ext.h
Set
PHP_FUNCTION (confirm_my_ext_compiled);/* For testing, remove later .*/
Change
PHP_FUNCTION (say_hello );
$ Vi ext/my_ext/my_ext.c
Set
Zend_function_entry php5cpp_functions [] = {
PHP_FE (confirm_my_ext_compiled, NULL)/* For testing, remove later .*/
{NULL, NULL, NULL}/* Must be the last line in php5cpp_functions [] */
};
Change
Zend_function_entry php5cpp_functions [] = {
PHP_FE (say_hello, NULL)
{NULL, NULL, NULL}/* Must be the last line in php5cpp_functions [] */
};
Add at the end:
PHP_FUNCTION (say_hello)
{
Zend_printf ("hello world \ n ");
}
4. Compile
$ Cd my_ext
$/Usr/local/php/bin/phpize
Ps: If: Cannot find autoconf ....... To install autoconf)
$./Configure -- with-php-config =/usr/local/php/bin/php-config
$ Make
At this time, my_ext/modules/my_ext.so will be compiled
5. Configure php. ini
Put my_ext.so in the/usr/local/php/ext/directory.
$ Vi php. ini
Add the following changes:
Extension_dir = '/usr/local/php/ext /'
Extension = my_ext.so
Vi. Test
$ Vi test. php
<? Php
Say_hello ();
?>
$/Usr/local/php/bin/php test. php
Hello world.
The success is achieved.