php 扩展开发(一)

ext_skel

php扩展生成工具
  • ./ext_skel –extname=module [–proto=file] [–stubs=file] [–xml[=file]] [–skel=dir] [–full-xml] [–no-help]
  • –extname=你的扩展名称
  • –proto=扩展函数定义文件
    扩展需要写一个函数定义文件,代码如下
string self_concat(string str, int n)
  • –stubs=只生成PHP_FE和PHP_FUNCTION单独的函数文件,单独拷贝使用
  • –xml 生成xml注释上传到phpdoc的版本管理中
  • –skel=生成骨架地址,php源码默认为/php-src/ext/skeleton中
  • –full-xml 未实现功能
  • –no-help 使用后生成代码省略很多注释
cat "string ccvita_string(string str)" > /tmp/ccvita.skel
cd php-src/ext
./ext_skel --extname=test5 --proto=/tmp/ccvita.skel

执行后生成扩展结构如下
这里写图片描述

  1. phpt为php测试文件
  2. php_test5.h 和 test5.c为源码文件,稍后分析
  3. config.m4 和 config.w32为 autoconf automake使用的配置文件
#dnl 为注释
dnl $Id$
dnl config.m4 for extension test5

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.

dnl If your extension references something external, use with:
# 这是一款比较强大的宏,用于将模块的描述 description 添加到“configure –help”命令的输出里面。PHP 会检查当前执行的 configure 脚本里面有没有–with-<modulename> 这个选项。 如果有则执行 unconditionaltest 语句(比如 –with-myext=yes 等), 此时,选项的值会被包含在 $withval 变量里面。否则就执行 conditionaltest 语句
dnl PHP_ARG_WITH(test5, for test5 support,
dnl Make sure that the comment is aligned:
dnl [  --with-test5             Include test5 support])

dnl Otherwise use enable:

dnl PHP_ARG_ENABLE(test5, whether to enable test5 support,
dnl Make sure that the comment is aligned:
dnl [  --enable-test5           Enable test5 support])

if test "$PHP_TEST5" != "no"; then
  dnl Write more examples of tests here...

  dnl # --with-test5 -> check with-path
  dnl SEARCH_PATH="/usr/local /usr"     # you might want to change this
  dnl SEARCH_FOR="/include/test5.h"  # you most likely want to change this
  dnl if test -r $PHP_TEST5/$SEARCH_FOR; then # path given as parameter
  dnl   TEST5_DIR=$PHP_TEST5
  dnl else # search default path list
  # 在执行 configure 命令时输出“checking <message>”等信息
  dnl   AC_MSG_CHECKING([for test5 files in default path])
  dnl   for i in $SEARCH_PATH ; do
  dnl     if test -r $i/$SEARCH_FOR; then
  dnl       TEST5_DIR=$i
  # 取得 AC_MSG_CHECKING 的执行结果,一般情况下 value 应为 yes 或 no
  dnl       AC_MSG_RESULT(found in $i)
  dnl     fi
  dnl   done
  dnl fi
  dnl
  dnl if test -z "$TEST5_DIR"; then
  dnl   AC_MSG_RESULT([not found])
  # 在执行 configure 命令时输出一条错误消息 message 并中止脚本的执行
  dnl   AC_MSG_ERROR([Please reinstall the test5 distribution])
  dnl fi

  dnl # --with-test5 -> add include path
  # 添加一条编译器的包含路径,比如用于模块需要为头文件添加搜索路径
  dnl PHP_ADD_INCLUDE($TEST5_DIR/include)

  dnl # --with-test5 -> check for lib and symbol presence
  dnl LIBNAME=test5 # you may want to change this
  dnl LIBSYMBOL=test5 # you most likely want to change this

  dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
  dnl [
  # 指定一个库的连接路径
  dnl   PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $TEST5_DIR/$PHP_LIBDIR, TEST5_SHARED_LIBADD)
  #向 php_config.h 添加一行定义:#define name value // description (这对模块的条件编译很有用)
  dnl   AC_DEFINE(HAVE_TEST5LIB,1,[ ])
  dnl ],[
  dnl   AC_MSG_ERROR([wrong test5 lib version or lib not found])
  dnl ],[
  dnl   -L$TEST5_DIR/$PHP_LIBDIR -lm
  dnl ])
  dnl
  dnl PHP_SUBST(TEST5_SHARED_LIBADD)
#这个是配置你的扩展时 PHP 必定调用的一个宏。你可以在模块名后面提供第二个参数,用来表明是否将其编译为动态共享模块。这会导致在编译时为你的源码提供一个 COMPILE_DL_<modulename> 的定义
  PHP_NEW_EXTENSION(test5, test5.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
fi

留下一个问题:什么是编译动态共享模块,静态模块,cgi程序?

  1. CREDITS为扩展备注信息,如开发者等
  2. EXPERIMENTAL为警告信息
  3. test5.php为php扩展测试程序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值