扩展入门较难,这里特殊推举淘宝大牛信海龙老师的入门课程,手把手教会你只要一块钱。
我们精选出文集中的第二章内容,以供免费试读。如果你不喜好付费课程,可以通过 PHP 手册第二部分进行学习(http://php.net/manual/zh/internals2.structure.php)
从hello world开始(试读)

以下内容以 PHP7 作为根本,讲解如何从零开始创建一个 PHP 扩展。示例中,我们将实现如下功能:
<?php
function say {
return \公众hello word\公众;
}
echo say;
?>
输出内容:
$ php ./test.php
$ hello word
在扩展中实现一个 say 方法,调用 say 方法后,输出 hello word。
第一步:天生代码
PHP 为我们供应了天生基本代码的工具 ext_skel。这个工具在 PHP 源代码的 ./ext 目录下。
$ cd php_src/ext/
$ ./ext_skel --extname=say
extname 参数的值便是扩展名称。实行 ext_skel 命令后,这样在当前目录下会天生一个与扩展名一样的目录。
第二步,修正 config.m4 配置文件
config.m4 的浸染便是合营 phpize 工具天生 configure 文件。configure 文件是用于环境检测的。检测扩展编译运行所需的环境是否知足。现在我们开始修正config.m4 文件。
$ cd ./say
$ vim ./config.m4
打开,config.m4 文件后,你会创造这样一段笔墨。
dnl If your extension references something external, use with:
dnl PHP_ARG_WITH(say, for say support,
dnl Make sure that the comment is aligned:
dnl [ --with-say Include say support])
dnl Otherwise use enable:
dnl PHP_ARG_ENABLE(say, whether to enable say support,
dnl Make sure that the comment is aligned:
dnl [ --enable-say Enable say support])
个中,dnl 是注释符号。
上面的代码说,如果你所编写的扩展如果依赖其它的扩展或者 lib 库,须要去掉 PHP_ARG_WITH 干系代码的注释。否则,去掉 PHP_ARG_ENABLE 干系代码段的注释。我们编写的扩展不须要依赖其他的扩展和 lib 库。
因此,我们去掉 PHP_ARG_ENABLE 前面的注释。去掉注释后的代码如下:
dnl If your extension references something external, use with:
dnl PHP_ARG_WITH(say, for say support,
dnl Make sure that the comment is aligned:
dnl [ --with-say Include say support])
dnl Otherwise use enable:
PHP_ARG_ENABLE(say, whether to enable say support,
Make sure that the comment is aligned:
[ --enable-say Enable say support])
第三步,代码实现
修正 say.c 文件。实现 say 方法。 找到PHP_FUNCTION(confirm_say_compiled),在其上面增加如下代码:
PHP_FUNCTION(say)
{
zend_string strg; strg = strpprintf(0, \公众hello word\公众);
RETURN_STR(strg);
}
找到 PHP_FE(confirm_say_compiled, 在上面增加如下代码:
PHP_FE(say, )
修正后的代码如下:
const zend_function_entry say_functions = {
PHP_FE(say, ) / For testing, remove later. /
PHP_FE(confirm_say_compiled, ) / For testing, remove later. /
PHP_FE_END / Must be the last line in say_functions /
};
/ }}} /
第四步,编译安装
编译扩展的步骤如下:
$ phpize
$ ./configure
$ make && make install
修正 php.ini 文件,增加如下代码:
[say]
extension = say.so
然后实行,php -m 命令。在输出的内容中,你会看到 say 字样。
第五步,调用测试
自己写一个脚本,调用 say 方法。看输出的内容是否符合预期。
<?php
echo say;
?>
圈主先容:
信海龙老师于 2013 年加入阿里巴巴,拥有十年互联网开拓履历。为帮助写 PHP 的小伙伴提高提高业务能力,特意在知加创建圈子『零根本学习PHP扩展开拓』,用于与 PHP 扩展开拓初学者分享、互换。
零根本学习 PHP 扩展开拓
也可点击【阅读原文】加入圈子