PHP扩展开发

首先需要确定系统中安装了gcc编译器,合适版本的bison等

####构建一个基本的扩展骨架 在PHP扩展开发时,使用ext_skel完成扩展的结构骨架创建。

1$ ./ext_skel 2./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]] 3 [--skel=dir] [--full-xml] [--no-help] 4 5 --extname=module 这里的module是要创建的扩展名称 6 --proto=file 这里的file文件包含了要创建的函数的原型 7 --stubs=file generate only function stubs in file 8 --xml generate xml documentation to be added to phpdoc-cvs 9 --skel=dir 创建扩展骨架的目录 10 --full-xml generate xml documentation for a self-contained extension (not yet implemented) 11 --no-help don't try to be nice and create comments in the code and helper functions to test if the module compiled

注意: ext_skel命令文件在源文件的ext目录下。

这里的--extname参数是要创建的扩展名称,扩展名称为 小写字母 + 下划线 组成,并且, 在ext目录中必须是唯一的。

例如,这里要创建一个名为ext_demo_1的PHP扩展:

1/vagrant/ext$ ./ext_skel --extname=ext_demo_1 2Creating directory ext_demo_1 3Creating basic files: config.m4 config.w32 .svnignore ext_demo_1.c php_ext_demo_1.h CREDITS EXPERIMENTAL tests/001.phpt ext_demo_1.php [done]. 4 5To use your new extension, you will have to execute the following steps: 6 71. $ cd .. 82. $ vi ext/ext_demo_1/config.m4 93. $ ./buildconf 104. $ ./configure --[with|enable]-ext_demo_1 115. $ make 126. $ ./php -f ext/ext_demo_1/ext_demo_1.php 137. $ vi ext/ext_demo_1/ext_demo_1.c 148. $ make 15 16Repeat steps 3-6 until you are satisfied with ext/ext_demo_1/config.m4 and 17step 6 confirms that your module is compiled into PHP. Then, start writing 18code and repeat the last two steps as often as necessary.

现在,在ext目录下出现了一个新建的扩展目录ext_demo_1

1/vagrant/ext/ext_demo_1$ ls 2config.m4 CREDITS ext_demo_1.c php_ext_demo_1.h 3config.w32 EXPERIMENTAL ext_demo_1.php tests

这时,该扩展是无法编译通过的,需要先编辑config.m4文件才行。

####配置文件config.m4

配置文件config.m4告诉UNIX构建系统扩展支持的configure选项以及扩展需要的额外的库, 包含哪些源文件等,该文件使用的是GNU的autoconf语法,以dnl开头的行为注释,使用中括号([和])包含的为字符串。

autoconf语法参见 AUTOCONF文档

1PHP_ARG_ENABLE(ext_demo_1, whether to enable ext_demo_1 support, 2[ --enable-ext_demo_1 Enable ext_demo_1 support]) 3 4if test "$PHP_EXT_DEMO_1" != "no"; then 5 PHP_SUBST(EXT_DEMO_1_SHARED_LIBADD) 6 PHP_NEW_EXTENSION(ext_demo_1, ext_demo_1.c, $ext_shared) 7fi

上述为autoconf的配置文件,第一个宏PHP_ARG_ENABLE,含有三个参数:

  • ext_demo_1 这是第一个参数,为./configure建立了名为enable-ext_demo_1的选项
  • 第二个参数将会在./configure命令处理到该扩展的配置文件时,显示该参数的内容
  • 第三个参数是./configure命令的帮助,在使用./configure --help的时候显示

第二个宏为PHP_NEW_EXTENSION,该宏声明了扩展的模块和必须要编译作为扩展一部分的源码文件。 如果需要多个源文件,则使用空格分隔,第三个参数$ext_shared与调用 PHP_SUBST(EXT_DEMO_1_SHARED_LIBADD)有关。

PHP_NEW_EXTENSION(ext_demo_1, ext_demo_1.c, $ext_shared)

####编译扩展

修改完config.m4文件之后,接下来编译PHP和扩展。

1/vagrant$ ./configure --disable-libxml --enable-ext_demo_1 --disable-dom --disable-simplexml --disable-xml --disable-xmlreader --disable-xmlwriter --without-pear --prefix=/usr/local/php 2/vagrant$ make 3/vagrant$ sudo make install 4Installing PHP SAPI module: cgi 5Installing PHP CGI binary: /usr/local/php/bin/ 6Installing PHP CLI binary: /usr/local/php/bin/ 7Installing PHP CLI man page: /usr/local/php/man/man1/ 8Installing build environment: /usr/local/php/lib/php/build/ 9Installing header files: /usr/local/php/include/php/ 10Installing helper programs: /usr/local/php/bin/ 11 program: phpize 12 program: php-config 13Installing man pages: /usr/local/php/man/man1/ 14 page: phpize.1 15 page: php-config.1 16/vagrant/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin 17ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar 18Installing PDO headers: /usr/local/php/include/php/ext/pdo/

此时,PHP安装在了/usr/local/php目录下,进入该目录,可以看到如下文件:

1/usr/local/php$ ls 2bin include lib man

进入/usr/local/php/bin目录,执行以下命令:

1/usr/local/php/bin$ ./php --info|grep demo 2Configure Command => './configure' '--disable-libxml' '--enable-ext_demo_1' '--disable-dom' '--disable-simplexml' '--disable-xml' '--disable-xmlreader' '--disable-xmlwriter' '--without-pear' '--prefix=/usr/local/php' 3ext_demo_1 4ext_demo_1 support => enabled

可以看到,phpinfo()中扩展支持已经启用了,按照上述步骤安装的扩展中包含了一个测试扩展是否能够正常工作的函数, 该函数名为confirm_ext_demo_1_compiled(arg),执行结果如下:

1/usr/local/php/bin$ ./php -r "echo confirm_ext_demo_1_compiled('mylxsw');" 2Congratulations! You have successfully modified ext/ext_demo_1/config.m4. Module mylxsw is now compiled into PHP.

可以看到,ext_demo_1扩展安装成功了。

我的博客:http://aicode.cc/,在这里可以看到更多相关文章。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

swap空间的增减方法

(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap