xinghe0 发表于 2017-3-4 12:50:41

PHP Extension 扩展开发

第 23 章 PHP Extension 扩展开发


  目录

1. Extension Example
2. PHP_MINFO_FUNCTION


1. Extension Example



  创建一个扩展只需要下面几个步骤


# cd php-5.4.14/ext/
# ./ext_skel --extname=netkiller
# cd netkiller
# /srv/php-5.4.8/bin/phpize
# ./configure --enable-netkiller --with-php-config=/srv/php-5.4.8/bin/php-config
# make
# make install

  下面我们开始分部讲解,首先创建一个扩展。


./ext_skel --extname=netkiller
Creating directory netkiller
Creating basic files: config.m4 config.w32 .cvsignore netkiller.c php_netkiller.h CREDITS EXPERIMENTAL tests/001.phpt netkiller.php .
To use your new extension, you will have to execute the following steps:
1.$ cd ..
2.$ vi ext/netkiller/config.m4
3.$ ./buildconf
4.$ ./configure ---netkiller
5.$ make
6.$ ./php -f ext/netkiller/netkiller.php
7.$ vi ext/netkiller/netkiller.c
8.$ make
Repeat steps 3-6 until you are satisfied with ext/netkiller/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


# vim netkiller/config.m4
PHP_ARG_ENABLE(netkiller, whether to enable netkiller support,
Make sure that the comment is aligned:
[--enable-netkiller         Enable netkiller support])

  去掉上面三行前的dnl, dnl 表示注释


# cat config.m4 | grep -v 'dnl'
PHP_ARG_ENABLE(netkiller, whether to enable netkiller support,
Make sure that the comment is aligned:
[--enable-netkiller         Enable netkiller support])
if test "$PHP_netkiller" != "no"; then
PHP_NEW_EXTENSION(netkiller, netkiller.c, $ext_shared)
fi

  执行 phpize


# cd netkiller
# /srv/php-5.4.8/bin/phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
# ./configure --enable-netkiller --with-php-config=/srv/php-5.4.8/bin/php-config
# make
# make install
# ls modules/
netkiller.lanetkiller.so

  创建 ini 文件


cat >> /srv/php/etc/conf.d/netkiller.ini <<PHP
extension=netkiller.so
PHP

  确认扩展看装成功


# /srv/php/bin/php -f netkiller.php
Functions available in the test extension:
confirm_netkiller_compiled
Congratulations! You have successfully modified ext/netkiller/config.m4. Module netkiller is now compiled into PHP.
# /srv/php-5.4.8/bin/php -m | grep netkiller
netkiller

  创建 php 测试程序


vim test.php
<?php
echo confirm_netkiller_compiled('netkiller');
phpinfo();

  输出结果


Congratulations! You have successfully modified ext/netkiller/config.m4. Module netkiller is now compiled into PHP.
netkiller
---------------------------
netkiller supportenabled
页: [1]
查看完整版本: PHP Extension 扩展开发