http://blog.zoeey.org/2012/05/07/note-of-php-extension-dev/
PHP扩展开发笔记
开发环境
CentOS 5.5 64-bit
PHP 5.3.12
为方便识别文章中PHP bin目录均使用绝对路径。自行编写时可加入$PATH。
从目录创建到执行测试
mkdir -p /tools/php/src
mkdir -p /tools/download
cd /tools/download
wget http://cn.php.net/distributions/php-5.3.12.tar.bz2
tar -xf /tools/download/php-5.3.12.tar.bz2 -C /tools/php/src/
cd /tools/php/src/php-5.3.12/
yum install libxml2-devel
HTTP测试需要cgi模式
./configure --prefix=/tools/php --enable-fastcgi --enable-debug
make
make install
cd /tools/php/src/php-5.3.12/ext
sh ./ext_skel --extname=learn
cd learn/
vim config.m4
去掉此两行前的 dnl
PHP_ARG_WITH(learn, for learn support,
[ --with-learn Include learn support])
/tools/php/bin/phpize
./configure --with-php-config=/tools/php/bin/php-config
make
make test
使用GDB调试PHP扩展
编写一个简单的测试(tests/confirm.phpt)
--TEST--
Check for learn confirm
--SKIPIF--
<?php if (!extension_loaded("learn")) print "skip"; ?>
--FILE--
<?php
echo confirm_learn_compiled('test');
?>
--EXPECT--
Congratulations! You have successfully modified ext/learn/config.m4. Module test is now compiled into PHP.
其他示例:
http://code.google.com/p/zoeeyphp/source/browse/#hg%2Fsrc%2Ftests
使用gdb调试
# yum install gdb
# gdb /tools/php/bin/php
(gdb) break learn.c:159
(gdb) run -f tests/confirm.phpt
(gdb) n
(gdb) bt
gdb手册 http://www.gnu.org/software/gdb/documentation/
PHP扩展内存泄露检测
# ./configure --enable-debug ...
# valgrind --tool=memcheck --leak-check=full php -f tests/router.php
# valgrind --tool=memcheck --leak-check=full --show-reachable=yes php -f tests/recorder.phpt
PHP扩展测试
# make test
# /tools/php/bin/pear run-tests tests/*.phpt
测试文件详情
http://qa.php.net/phpt_details.php
PHP扩展HTTP请求测试
PHP扩展上传测试
http://qafoo.com/blog/013_testing_file_uploads_with_php.html
参考资料
浅析 PHP 官方自动化测试方法
http://www.ibm.com/developerworks/cn/opensource/os-cn-php-autotest/index.html
PHP扩展开发四部曲
http://www.snailinaturtleneck.com/blog/2011/08/11/php-extensions-made-eldrich-installing-php/
http://www.snailinaturtleneck.com/blog/2011/08/11/php-extensions-made-eldrich-hello-world/
http://www.snailinaturtleneck.com/blog/2011/08/11/php-extensions-made-eldrich-php-variables/
http://www.snailinaturtleneck.com/blog/2011/08/11/php-extensions-made-eldrich-classes/
推荐手册
Sams.Extending.and.Embedding.PHP.May.2006.chm
Zend.com 扩展开发
http://devzone.zend.com/303/extension-writing-part-i-introduction-to-php-and-zend/
http://devzone.zend.com/317/extension-writing-part-ii-parameters-arrays-and-zvals/
http://devzone.zend.com/446/extension-writing-part-iii-resources/