欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 编程语言 > php >内容正文

php

php扩展开发 -凯发ag旗舰厅登录网址下载

发布时间:2025/1/21 php 17 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 php扩展开发 - 构建第一个php扩展 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘python工程师标准>>>

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

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

$ ./ext_skel ./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]][--skel=dir] [--full-xml] [--no-help]--extname=module 这里的module是要创建的扩展名称--proto=file 这里的file文件包含了要创建的函数的原型--stubs=file generate only function stubs in file--xml generate xml documentation to be added to phpdoc-cvs--skel=dir 创建扩展骨架的目录--full-xml generate xml documentation for a self-contained extension (not yet implemented)--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扩展:

/vagrant/ext$ ./ext_skel --extname=ext_demo_1 creating directory ext_demo_1 creating 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].to use your new extension, you will have to execute the following steps:1. $ cd .. 2. $ vi ext/ext_demo_1/config.m4 3. $ ./buildconf 4. $ ./configure --[with|enable]-ext_demo_1 5. $ make 6. $ ./php -f ext/ext_demo_1/ext_demo_1.php 7. $ vi ext/ext_demo_1/ext_demo_1.c 8. $ makerepeat steps 3-6 until you are satisfied with ext/ext_demo_1/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.

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

/vagrant/ext/ext_demo_1$ ls config.m4 credits ext_demo_1.c php_ext_demo_1.h config.w32 experimental ext_demo_1.php tests

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

####配置文件config.m4

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

autoconf语法参见 autoconf文档

php_arg_enable(ext_demo_1, whether to enable ext_demo_1 support, [ --enable-ext_demo_1 enable ext_demo_1 support])if test "$php_ext_demo_1" != "no"; thenphp_subst(ext_demo_1_shared_libadd)php_new_extension(ext_demo_1, ext_demo_1.c, $ext_shared) fi

上述为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和扩展。

/vagrant$ ./configure --disable-libxml --enable-ext_demo_1 --disable-dom --disable-simplexml --disable-xml --disable-xmlreader --disable-xmlwriter --without-pear --prefix=/usr/local/php /vagrant$ make /vagrant$ sudo make install installing php sapi module: cgi installing php cgi binary: /usr/local/php/bin/ installing php cli binary: /usr/local/php/bin/ installing php cli man page: /usr/local/php/man/man1/ installing build environment: /usr/local/php/lib/php/build/ installing header files: /usr/local/php/include/php/ installing helper programs: /usr/local/php/bin/program: phpizeprogram: php-config installing man pages: /usr/local/php/man/man1/page: phpize.1page: php-config.1 /vagrant/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar installing pdo headers: /usr/local/php/include/php/ext/pdo/

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

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

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

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

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

/usr/local/php/bin$ ./php -r "echo confirm_ext_demo_1_compiled('mylxsw');" congratulations! you have successfully modified ext/ext_demo_1/config.m4. module mylxsw is now compiled into php.

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

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

转载于:https://my.oschina.net/agiledev/blog/343162

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的php扩展开发 - 构建第一个php扩展的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

网站地图