taoCMS是基于php+sqlite/mysql的国内最小(100Kb左右)的功能完善、开源免费的CMS管理系统

PHP扩展开发笔记

2013-08-10

由 moxie 撰写  

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/

类别:技术文章 | 阅读:313776 | 评论:0 | 标签:php php扩展

想收藏或者和大家分享这篇好文章→

“PHP扩展开发笔记”共有0条留言

发表评论

姓名:

邮箱:

网址:

验证码:

公告

taoCMS发布taoCMS 3.0.2(最后更新21年03月15日),请大家速速升级,欢迎大家试用和提出您宝贵的意见建议。

捐助与联系

☟请使用新浪微博联系我☟

☟在github上follow我☟

标签云