Div*_*com 4 php closures anonymous-function
如何确定闭包声明的参数个数以便在闭包之外使用?例如:
$myClosure = function($arg1, $arg2, $arg3){
}
$numArgs = someMagicalFunction($myClosure);
echo("that closure expects $numArgs arguments");
Run Code Online (Sandbox Code Playgroud)
是否有一些功能可以满足我的需求?
使用反射.请参阅此文章:http://www.bossduck.com/2009/07/php-5-3-closures-and-reflection/
$func = function($one, $two = 'test') {
echo 'test function ran'.PHP_EOL;
};
$info = new ReflectionFunction($func);
var_dump(
$info->getName(),
$info->getNumberOfParameters(),
$info->getNumberOfRequiredParameters()
);
Run Code Online (Sandbox Code Playgroud)
哪个回报:
string(9) "{closure}"
int(2)
int(1)
Run Code Online (Sandbox Code Playgroud)