PHP扩展生成框架的一个简便功能

原文

http://hi.baidu.com/thinkinginlamp/item/0c010e4d6b2af20ae935047f



PHP扩展的生成框架(ext_skel)已经很方便了,今天我发现一个选项可以让开发过程更方便。

./ext_skel --help

./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
           [--skel=dir] [--full-xml] [--no-help]

  --extname=module   module is the name of your extension
  --proto=file       file contains prototypes of functions to create
  --stubs=file       generate only function stubs in file
  --xml              generate xml documentation to be added to phpdoc-cvs
  --skel=dir         path to the skeleton directory
  --full-xml         generate xml documentation for a self-contained extension
                     (not yet implemented)
  --no-help          don't try to be nice and create comments in the code
                     and helper functions to test if the module compiled

首先,编写一个proto文件:

vi foo.proto

内容如下(注意行尾没有分号):

int fun_a(string str_a, string str_b)
int fun_b(int int_a, int int_b)

软后生成扩展:

./ext_skel --extname=foo --proto=foo.proto

然后看看生成的文件:

vi php_foo.h

你会发现相应的内容已经被自动添加了:

PHP_FUNCTION(confirm_foo_compiled); /* For testing, remove later. */
PHP_FUNCTION(fun_a);
PHP_FUNCTION(fun_b);

vi foo.c

/* {{{ foo_functions[]
 *
 * Every user visible function must have an entry in foo_functions[].
 */
zend_function_entry foo_functions[] = {
    PHP_FE(confirm_foo_compiled,    NULL)       /* For testing, remove later. */
    PHP_FE(fun_a,   NULL)
    PHP_FE(fun_b,   NULL)
    {NULL, NULL, NULL}  /* Must be the last line in foo_functions[] */
};

/* {{{ proto int fun_a(string str_a, string str_b)
    */
PHP_FUNCTION(fun_a)
{
    char *str_a = NULL;
    char *str_b = NULL;
    int argc = ZEND_NUM_ARGS();
    int str_a_len;
    int str_b_len;

    if (zend_parse_parameters(argc TSRMLS_CC, "ss", &str_a, &str_a_len, &str_b, &str_b_len) == FAILURE)
        return;

    php_error(E_WARNING, "fun_a: not yet implemented");
}
/* }}} */

/* {{{ proto int fun_b(int int_a, int int_b)
    */
PHP_FUNCTION(fun_b)
{
    int argc = ZEND_NUM_ARGS();
    long int_a;
    long int_b;

    if (zend_parse_parameters(argc TSRMLS_CC, "ll", &int_a, &int_b) == FAILURE)
        return;

    php_error(E_WARNING, "fun_b: not yet implemented");
}
/* }}} */

可以看到,相应的代码都已经自动加上了。

如果我们在foo.proto里加入

int fun_c(resource res_a, array arr_b[, int int_c])

那么:

/* {{{ proto int fun_c(resource res_a, array arr_b [, int int_c])
    */
PHP_FUNCTION(fun_c)
{
    int argc = ZEND_NUM_ARGS();
    int res_a_id = -1;
    long int_c;
    zval *res_a = NULL;
    zval *arr_b = NULL;

    if (zend_parse_parameters(argc TSRMLS_CC, "ra|l", &res_a, &arr_b, &int_c) == FAILURE)
        return;

    if (res_a) {
        ZEND_FETCH_RESOURCE(???, ???, res_a, res_a_id, "???", ???_rsrc_id);
    }

    php_error(E_WARNING, "fun_c: not yet implemented");
}
/* }}} */

可以看到,可选参数也判断出来了,很智能。

贴几个网址:

http://talks.php.net/show/extending-php-apachecon2003/

http://blog.csdn.net/taft/archive/2006/02/10/596291.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值