编程语言
首页 > 编程语言> > 如何作为参数传递PHPDoc匿名函数?

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

作者:互联网

当它作为参数传递时,我应该如何记录匿名函数?例如:

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

提前致谢.

解决方法:

要记录函数接受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; };

标签:php,closures,phpdoc
来源: https://codeday.me/bug/20190702/1356354.html