Home | Mirror | SearchITEYE 博客 | OSChina 博客 | 51CTO 博客

第 9 章 PHP Extension 扩展开发

目录

9.1. Extension Example
9.2. PHP_FUNCTION
9.3. PHP_INI
9.4. PHP_MINFO_FUNCTION

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

	
# 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
	
	

9.1. Extension Example

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

		
./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 [done].

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 --[with|enable]-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.la  netkiller.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 support	enabled
		
		
comments powered by Disqus