1. First, you need to install PHP
2. Enter the ext directory under the PHP download and decompression directory
#cd /root/php-5.3.6/ext
#./ext_skel --extname=hmc
Description:
./ext_skel --extname=module_name
module_name is the name of the extension module that you can choose, such as the hmc I chose. After executing the tool, a directory with the module_name of your choice will be automatically created in the ext directory. Relevant codes have been generated in it. In these codes, you only need to adjust the three lines of comments in the config.m4 file to compile normally with this customization. Extension module for PHP.
3. Modify config.m4
You can copy the generated module directory to wherever you want to place it
Enter the hmc directory
vi config.m4
Open the config.m4 file using a text editor
Place
according to your own choice
dnl PHP_ARG_WITH(hmc, for hmc support,
dnl Make sure that the comment is aligned:
dnl [ --with-hmc Include hmc support])
Modify to
PHP_ARG_WITH(hmc for hmc support,
Make sure that the comment is aligned:
[ --with-hmc Include hmc support])
Or replace
dnl PHP_ARG_ENABLE(hmc, whether to enable hmc support,
dnl Make sure that the comment is aligned:
dnl [ --enable-hmc Enable hmc support])
Modify to
PHP_ARG_ENABLE(hmc, whether to enable hmc support,
Make sure that the comment is aligned:
[ --enable-hmc Enable hmc support])
Normally I would choose the latter, then save and exit.
4. Modify the .c and .h file codes
Vi hmc.c
Modify the following code in the file
/* Every user visible function must have an entry in hmc_functions[].
*/
const zend_function_entry hmc_functions[] = {
PHP_FE(say_hello, NULL) /* Add a line of code */
PHP_FE(confirm_hmc_compiled, NULL) /* For testing, remove later. */
{NULL, NULL, NULL} /* Must be the last line in hmc_functions[] */
};
Add the following code at the end of the file
PHP_FUNCTION(say_hello)
{
zend_printf("hello worldn");
}
Save the file and exit
vi php_hmc.h
In the file, add the following code before the line PHP_FUNCTION(confirm_hmc_compiled);
PHP_FUNCTION(say_hello);
Save the file and exit
5. Compile and install
#phpize
#./configure --enable-hmc
#make
#make install
There should be a hmc.so file in the modules directory of php. The location will be different on different machines
6. Modify PHP configuration
Edit php.ini and add the extension:
Add under [PHP] module:
extension = hmc.so
Restart Httpd
#service httpd restart
7.php call
Create a c.php file in the corresponding www directory
say_hello();
?>
At this time the browser should output:
hello world
8. Debugging
Can be entered on the command line
#php –q c.php
Need to enter the current www directory
To check whether the module is installed, you can use phpinfo()
You can also use the php -m command
Use first
#which php
Find the directory where php is located, the directory is different on different machines
#/usr/bin/php -m
9. Function with parameters
Vi hmc.c
Modify the final say_hello function content 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, %sn", Z_STRVAL_PP(yourname));
}
Modify c.php to
say_hello("Qing Qing Yue'er");
?>