Php 匿名函数参数信息不正确
我需要根据匿名函数的参数编写代码。以下代码正确给出了参数 示例代码1:Php 匿名函数参数信息不正确,php,parameters,closures,Php,Parameters,Closures,我需要根据匿名函数的参数编写代码。以下代码正确给出了参数 示例代码1: class test{ } $func = function($a, $b, test $t, $c = 'new') { echo 'test function ran'.PHP_EOL; }; $info = new ReflectionFunction($func); var_dump( $info->getName(), $info->getNumberOfParamet
class test{
}
$func = function($a, $b, test $t, $c = 'new') {
echo 'test function ran'.PHP_EOL;
};
$info = new ReflectionFunction($func);
var_dump(
$info->getName(),
$info->getNumberOfParameters(),
$info->getNumberOfRequiredParameters()
);
结果变量转储
string(9) "{closure}"
int(4)
int(3)
string(9) "{closure}"
int(4)
int(4)
当我改变参数顺序时,结果是不同的。你有这方面的信息吗
示例代码2:
class test{
}
$func = function($a, $b, $c = 'new', test $t ) {
echo 'test function ran'.PHP_EOL;
};
$info = new ReflectionFunction($func);
var_dump(
$info->getName(),
$info->getNumberOfParameters(),
$info->getNumberOfRequiredParameters()
);
结果变量转储
string(9) "{closure}"
int(4)
int(3)
string(9) "{closure}"
int(4)
int(4)
对不起,我的英语不好。我希望我能告诉你
---更新--
谢谢你的回答。我现在明白了。
第二个例子也很好。调用函数时,不能忽略
$c
参数。。。是必需的参数。不能调用$func(1,2,$objTest)
;必须为$c
指定一个参数,就像$func(1,2,null,$objTest)
$c有一个默认值。为什么需要这样做?因为对象先到?$c
是必需的,因为第四个参数是必需的。您不能执行$func(1,2,$t)
,因为$t
将是第三个参数。第二个示例也很好。调用函数时,不能忽略$c
参数。。。是必需的参数。不能调用$func(1,2,$objTest)
;必须为$c
指定一个参数,就像$func(1,2,null,$objTest)
$c有一个默认值。为什么需要这样做?因为对象先到?$c
是必需的,因为第四个参数是必需的。您不能执行$func(1,2,$t)
,因为$t
将是第三个参数。