1. 首先是要安装好 PHP
2. 进入 PHP 的下载解压目录下的 ext 目录
- #cd /root/php-5.3.6/ext
- #./ext_skel --extname=hmc
说明:
./ext_skel --extname=module_name
module_name 是你自己可以选择的扩展模块的名字, 例如我选择的 hmc 执行工具后会自动在 ext 目录下建立你选择的 module_name 名字的目录, 里面已经生成了相关的代码, 这些代码中只需要调整 config.m4 文件中的三行注释就可以正常的编译带这个自定义扩展模块的 php 了
3. 修改 config.m4
可以将生成的 module 目录复制到任意希望放置的地方
进入 hmc 目录
vi config.m4
使用文本编辑器打开 config.m4 文件
根据你自己的选择将
- dnl PHP_ARG_WITH(hmc, for hmc support,
- dnl Make sure that the comment is aligned:
- dnl [--with-hmc Include hmc support])
修改成
- PHP_ARG_WITH(hmc for hmc support,
- Make sure that the comment is aligned:
- [ --with-hmc Include hmc support])
或者将
- dnl PHP_ARG_ENABLE(hmc, whether to enable hmc support,
- dnl Make sure that the comment is aligned:
- dnl [ --enable-hmc Enable hmc support])
修改成
- PHP_ARG_ENABLE(hmc, whether to enable hmc support,
- Make sure that the comment is aligned:
- [ --enable-hmc Enable hmc support])
一般我会选择后者, 然后保存退出
4. 修改. c 和. h 文件代码
Vi hmc.c
将文件其中的下列代码进行修改
- /* Every user visible function must have an entry in hmc_functions[].
- */
- const zend_function_entry hmc_functions[] = {
- PHP_FE(say_hello, NULL) /* 添加着一行代码 */
- PHP_FE(confirm_hmc_compiled, NULL) /* For testing, remove later. */
- {NULL, NULL, NULL} /* Must be the last line in hmc_functions[] */
- };
在文件的最后添加下列代码
- PHP_FUNCTION(say_hello)
- {
- zend_printf("hello world\n");
- }
保存文件退出
vi php_hmc.h
在文件中 PHP_FUNCTION(confirm_hmc_compiled); 一行前面添加下面的代码
PHP_FUNCTION(say_hello);
保存文件退出
5. 编译安装
- #phpize
- #./configure --enable-hmc
- #make
- #make install
应该在 php 的 modules 目录下有一个 hmc.so 文件, 不同的机器位置会不一样
6. 修改 PHP 配置
编辑 php.ini, 把扩展加入进去:
在 [PHP] 模块下增加:
extension = hmc.so
重启 Httpd
#service httpd restart
7.php 调用
建立一个 c.php 文件在对应的 www 目录下
say_hello();
?>
这时浏览器应该输出:
hello world
8. 调试
可以在命令行下输入
#php q c.php
需要进入当前的 www 目录下
查看是否安装好模块可以用 phpinfo()
也可以适用 php -m 命令
先用
#which php
找到 php 所在目录, 不同机器目录不一样
#/usr/bin/php -m
9. 带参数的函数
Vi hmc.c
修改最后的 say_hello 函数内容如下:
- 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, %s\n", Z_STRVAL_PP(yourname));
- }
修改 c.php 为
- say_hello(清清月儿 ");
- ?>
来源: https://www.php1.cn/detail/php-851814bb09.html