您的位置:首页 > 编程语言 > PHP开发

php 创建一个扩展开发环境的正确姿势

2017-06-01 12:13 645 查看

安装编译PHP

官网下载最新php-7.1.5.tar的源码包

设置开发目录,打开调试模式,禁止其他内置模块

cd path/to/php-src
./configure --prefix=path/to/php --enable-debug --disable-all
make && make install


生成PHP扩展目录

cd php-src/ext
./ext_skel --extname=my_helloworld
Creating directory my_helloworld
Creating basic files: config.m4 config.w32 .gitignore my_helloworld.c php_my_helloworld.h CREDITS EXPERIMENTAL tests/001.phpt my_helloworld.php [done].

To use your new extension, you will have to execute the following steps:

1.  $ cd ..
2.  $ vi ext/my_helloworld/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-my_helloworld
5.  $ make
6.  $ ./sapi/cli/php -f ext/my_helloworld/my_helloworld.php
7.  $ vi ext/my_helloworld/my_helloworld.c
8.  $ make

Repeat steps 3-6 until you are satisfied with ext/my_helloworld/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.


编辑扩展文件config.m4

去掉这几行的 dnl 注释前缀

PHP_ARG_ENABLE(my_helloworld, whether to enable my_helloworld support,
Make sure that the comment is aligned:
[  --enable-my_helloworld           Enable my_helloworld support])


编译扩展

cd my_helloworld
/path/to/php/bin/phpize
Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303

./configure --enable-my_helloworld --with-php-config=/path/to/php/bin/php-config
make && make install


测试扩展

修改php.ini文件,添加一行记录

extension=my_helloworld.so

测试安装结果

path/to/bin/php my_helloworld.php
Functions available in the test extension:
confirm_my_helloworld_compiled

Congratulations! You have successfully modified ext/my_helloworld/config.m4. Module my_helloworld is now compiled into PHP.

测试加载模块

path/to/bin/php -m
[PHP Modules]
Core
date
my_helloworld
pcre
Reflection
SPL
standard

[Zend Modules]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: