PHP 扩展开发的文章,我均已更新至《TIPI》(下面的博文可能已经过时,以 TIPI 上的内容为准)。
通过原型来生成扩展框架[root@localhost ext]# mkdir zmk_test
[root@localhost ext]# cd zmk_test/
[root@localhost zmk_test]# vim test.protoint zmk_test_inc(int a)
然后通过下面的命令就能生成扩展了[root@localhost ext]# ./ext_skel --extname=zmktest1 --proto=./zmk_test/test.proto
...
[root@localhost ext]# cd zmktest1/
然后vim zmktest1.c,发现我们原型设定的函数已经给我们生成好了,参数名都一样。PHP_FUNCTION(zmk_test_inc)
{
int argc = ZEND_NUM_ARGS();
zend_long a;
if (zend_parse_parameters(argc TSRMLS_CC, "l", &a) == FAILURE)
return;
php_error(E_WARNING, "zmk_test_inc: not yet implemented");
}
稍微修改下PHP_FUNCTION(zmk_test_inc)
{
int argc = ZEND_NUM_ARGS();
zend_long a;
if (zend_parse_parameters(argc TSRMLS_CC, "l", &a) == FAILURE)
return;
//php_error(E_WARNING, "zmk_test_inc: not yet implemented");
RETURN_LONG(a+1);
}
然后就是和上面的扩展开发测试的一样,修改config.m4->phpize->make
这里我学习编写下单元测试,进入到tests目录,添加一个测试文件002.phpt,测试上面新增的方法,zmk_test_inc(20)期望值当然是21啦--TEST--
Check for function zmk_test_inc
--SKIPIF--
--FILE--
echo zmk_test_inc(20);
?>
--EXPECT--
21
然后make test,都通过啦[root@localhost zmktest1]# make test
Build complete.
Don't forget to run 'make test'.
=====================================================================
PHP : /usr/local/php7/bin/php
PHP_SAPI : cli
PHP_VERSION : 7.0.2
ZEND_VERSION: 3.0.0
PHP_OS : Linux - Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64
INI actual : /root/lnmp1.2-full/php-7.0.2/ext/zmktest1/tmp-php.ini
More .INIs :
CWD : /root/lnmp1.2-full/php-7.0.2/ext/zmktest1
Extra dirs :
VALGRIND : Not used
=====================================================================
TIME START 2016-01-27 16:54:42
=====================================================================
PASS Check for zmktest1 presence [tests/001.phpt]
PASS Check for function zmk_test_set [tests/002.phpt]
=====================================================================
TIME END 2016-01-27 16:54:42
=====================================================================
TEST RESULT SUMMARY
---------------------------------------------------------------------
Exts skipped : 0
Exts tested : 44
---------------------------------------------------------------------
Number of tests : 2 2
Tests skipped : 0 ( 0.0%) --------
Tests warned : 0 ( 0.0%) ( 0.0%)
Tests failed : 0 ( 0.0%) ( 0.0%)
Expected fail : 0 ( 0.0%) ( 0.0%)
Tests passed : 2 (100.0%) (100.0%)
---------------------------------------------------------------------
Time taken : 0 seconds
=====================================================================
This report can be automatically sent to the PHP QA team at
http://qa.php.net/reports and http://news.php.net/php.qa.reports
This gives us a better understanding of PHP's behavior.
If you don't want to send the report immediately you can choose
option "s" to save it.You can then email it to qa-reports@lists.php.net later.
Do you want to send this report now? [Yns]: n
[root@localhost zmktest1]# make install
Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-zts-20151012/
关于报错检出
在我们自己的扩展目录里会有一个和扩展同名的php文件,也可以在那里面测试,make test不会报错,但是在php文件里运行就会有比较多的报错信息了。php -d"extension=zmktest1.so" zmktest1.php
同时也可以在编译的时候加上--enable-debug