1,下载php源码
git clone https://github.com/php/php-src.git
2,创建扩展
PHP提供了一个命令用于生成扩展的基本文件架构,只要调用该命令即可。
cd php-src/ext/
./ext_skel --extname=php_hello
选择自已喜欢的编辑器,打开 php-src/ext/php_hello目录,上面的命令已经帮我们生成好了一些文件。
最主要的是 config.m4, php_php_hello.h, php_hello.c 这三个文件。
config.m4 编译配置文件
php_php_hello.h 扩展头文件
php_hello.c 扩展源码
3,修改config.m4
PHP_ARG_ENABLE(php_hello, whether to enable php_hello support,
Make sure that the comment is aligned:
[ --enable-php_hello Enable php_hello support])
if test "$PHP_PHP_HELLO" != "no"; then
PHP_NEW_EXTENSION(php_hello, php_hello.c, $ext_shared)
fi
4,修改 php_hello.c
PHP_FUNCTION(confirm_php_hello_compiled)
{
php_printf("hello world!\n");
return;
}
5,编译扩展
cd php_hello
phpize
./configure --with-php-config=/usr/bin/php-config
make
sudo make install
6,运行测试文件
php -q php_hello.php
7,运行结果
Functions available in the test extension:
confirm_php_hello_compiled
hello world!