Press "Enter" to skip to content

Zend Parameters Parser新增类型描述符介绍

从PHP5.3开始, zend_parse_paramters_*函数新增了如下几个新的类型描述符:

f  - function or array containing php method call info (returned as
      zend_fcall_info and zend_fcall_info_cache)
H  - array or HASH_OF(object) (returned as HashTable*)
L  - long, limits out-of-range numbers to LONG_MAX/LONG_MIN (long)
Z  - the actual zval (zval**)
*  - variable arguments list (0 or more)
+  - variable arguments list (1 or more)

这也使得我们做扩展开发的时候, 能更加轻松的处理输入参数, 得到想要的值.
比如没有f之前, 如果我们扩展中提供一个方法, 接受用户传入的一个回调函数, 那我们需要对用户传入的参数做判断:

1. 是字符串还是数组
2. 如果是字符串, 那么是否是一个可调用的回调函数
3. 如果是数组, 那么就分为, 调用类的静态方法, 和一个对象的方法俩种情况.
    并验证他们是否是可调用的回调函数.

这个挺麻烦的吧? 而有了f, 我们就可以简单的:

PHP_FUNCTION(dummy) {
    zval *retval_ptr = NULL;
    zend_fcall_info fci;
    zend_fcall_info_cache fci_cache;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
            "f*", &fci, &fci_cache, &fci.params, &fci.param_count) == FAILURE) {
        return;
    }
    fci.retval_ptr_ptr = &retval_ptr;
    zend_call_function(&fci, &fci_cache TSRMLS_CC);
    .....
}

是不是方便了很多呢?
另外, 上面的例子也顺便介绍了"*", 这个主要用来方便可变参数函数的开发. "+"也类似.
最后要说的就是"H".
首先我们要说说PHP的一些历史原因, 导致PHP的对象, 其实也可以作为数组来使用, 这个时候默认的它的属性就会作为数组载体.
这样, 当我们在扩展中的一个函数中申明要得到一个数组, 对于如下的object, 就无法直接调用:

$person = new StdClass();
$person->name = "Laruence";
$person->age   = 28;

而如果我们在扩展的函数中使用"H"来申明我要得到一个数组, 那么如上的对象, 就可以直接作为合法参数使用了.

4 Comments

  1. best college essay
    best college essay May 24, 2019

    I want to show my thanks to the writer just for rescuing me from this matter. After exploring through the online world and meeting basics which were not powerful, I figured my entire life was done. Living without the approaches to the issues you have fixed all through your report is a critical case, and the ones that would have adversely affected my career if I had not discovered your website. Your good natural talent and kindness in playing with a lot of things was tremendous. I am not sure what I would’ve done if I hadn’t encountered such a point like this. It’s possible to at this point look ahead to my future. Thanks so much for your reliable and amazing guide. I will not think twice to recommend the sites to anyone who would need support on this issue.

  2. […] 另外, 对于扩展开发来说, zend_parse_parameters也提供了对应的新标志:”f”, 可以参看Zend Parameters Parser新增类型描述符介绍 […]

  3. 宁波纹身
    宁波纹身 July 6, 2011

    不怎么明白,博主能否讲的详细点呢?

Comments are closed.