有没有办法检测类中函数的参数数量?
我想做的是以下内容.
$class = 'foo';
$path = 'path/to/file';
if ( ! file_exists($path)) {
die();
}
require($path);
if ( ! class_exists($class)) {
die();
}
$c = new class;
if (num_function_args($class, $function) == count($url_segments)) {
$c->$function($one, $two, $three);
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?
使用反射,但这实际上是代码中的开销; 并且方法可以具有任意数量的参数,而不在方法定义中明确定义它们.
$classMethod = new ReflectionMethod($class,$method);
$argumentCount = count($classMethod->getParameters());
Run Code Online (Sandbox Code Playgroud)
要获取Function或Method签名中的参数数量,您可以使用
例
$rf = new ReflectionMethod('DateTime', 'diff');
echo $rf->getNumberOfParameters(); // 2
echo $rf->getNumberOfRequiredParameters(); // 1
Run Code Online (Sandbox Code Playgroud)
要获取在运行时传递给函数的参数数量,您可以使用
func_num_args - 返回传递给函数的参数数量例
function fn() {
return func_num_args();
}
echo fn(1,2,3,4,5,6,7); // 7
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5254 次 |
| 最近记录: |