如何作为参数传递PHPDoc匿名函数?

时间:2015-12-30 19:12:41

标签: php closures phpdoc

如果匿名函数作为参数传递,我该如何记录?例如:

// Call my_function(), passing 2 arguments.
my_function( 'foo', function() {
    // Body of the anon function I'd like to document.
} );

提前致谢。

2 个答案:

答案 0 :(得分:2)

要记录函数接受Closure,我建议callable

/**
 * Do something.
 * @param callable $code
 */
function foo(callable $code) {
}

关于评论,PHPDoc使用DocBlocks,PHP引擎Tokenizer只识别正式定义。因此,PHPDoc不会看到这个:

/**
 * My closure.  PHPDoc will *not* parse this, because it's not a formal definition.
 * @param string $name
 */
$closure = function ($name) { return $name; };

答案 1 :(得分:0)

到目前为止,这对我有用:

interface CustomCallback
{
    /**
     * @return string
     */
    public function __invoke();
}

/**
 * @param string $a
 * @param CustomCallback $b
 * @return void
 */
my_function($a, $b) {

}

这告诉我们my_function的第二个参数期望实现CustomCallback接口的东西。通过查看CustomCallback接口,我们可以看到由于__invoke方法它是可调用的。然后,通过查看__invoke方法的文档,我们可以看到它所期望的参数以及返回类型。

相关问题