Is there a way to detect the number of arguments a function in a class has?
是否有一种方法可以检测一个类中的函数有多少个参数?
What I want to do is the following.
我想做的是下面这些。
$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);
}
Is this possible?
这是可能的吗?
3 个解决方案
#1
5
Using reflection, but this is really an overhead in your code; and a method can have any number of arguments without their explicitly being defined in the method definition.
使用反射,但这实际上是代码中的开销;一个方法可以有任意数量的参数,而不需要在方法定义中显式地定义它们。
$classMethod = new ReflectionMethod($class,$method);
$argumentCount = count($classMethod->getParameters());
#2
5
To get the number of arguments in a Function or Method signature, you can use
要获取函数或方法签名中的参数数,可以使用
- ReflectionFunctionAbstract::getNumberOfParameters - Gets number of parameters
- getNumberOfParameters—获取参数的个数
Example
例子
$rf = new ReflectionMethod('DateTime', 'diff');
echo $rf->getNumberOfParameters(); // 2
echo $rf->getNumberOfRequiredParameters(); // 1
To get the number of arguments passed to a function at runtime, you can use
要获取在运行时传递给函数的参数数,可以使用
-
func_num_args
— Returns the number of arguments passed to the function - func_num_args——返回传递给函数的参数个数
Example
例子
function fn() {
return func_num_args();
}
echo fn(1,2,3,4,5,6,7); // 7
#3
3
Use call_user_func_array instead, if you pass too many parameters, they will simply be ignored.
相反,使用call_user_func_array,如果您传递了太多的参数,它们将被忽略。
Demo
演示
class Foo {
public function bar($arg1, $arg2) {
echo $arg1 . ', ' . $arg2;
}
}
$foo = new Foo();
call_user_func_array(array($foo, 'bar'), array(1, 2, 3, 4, 5));
Will output
将输出
1, 2
For dynamic arguments, use func_get_args like this :
对于动态参数,使用func_get_args如下:
class Foo {
public function bar() {
$args = func_get_args();
echo implode(', ', $args);
}
}
$foo = new Foo();
call_user_func_array(array($foo, 'bar'), array(1, 2, 3, 4, 5));
Will output
将输出
1, 2, 3, 4, 5
#1
5
Using reflection, but this is really an overhead in your code; and a method can have any number of arguments without their explicitly being defined in the method definition.
使用反射,但这实际上是代码中的开销;一个方法可以有任意数量的参数,而不需要在方法定义中显式地定义它们。
$classMethod = new ReflectionMethod($class,$method);
$argumentCount = count($classMethod->getParameters());
#2
5
To get the number of arguments in a Function or Method signature, you can use
要获取函数或方法签名中的参数数,可以使用
- ReflectionFunctionAbstract::getNumberOfParameters - Gets number of parameters
- getNumberOfParameters—获取参数的个数
Example
例子
$rf = new ReflectionMethod('DateTime', 'diff');
echo $rf->getNumberOfParameters(); // 2
echo $rf->getNumberOfRequiredParameters(); // 1
To get the number of arguments passed to a function at runtime, you can use
要获取在运行时传递给函数的参数数,可以使用
-
func_num_args
— Returns the number of arguments passed to the function - func_num_args——返回传递给函数的参数个数
Example
例子
function fn() {
return func_num_args();
}
echo fn(1,2,3,4,5,6,7); // 7
#3
3
Use call_user_func_array instead, if you pass too many parameters, they will simply be ignored.
相反,使用call_user_func_array,如果您传递了太多的参数,它们将被忽略。
Demo
演示
class Foo {
public function bar($arg1, $arg2) {
echo $arg1 . ', ' . $arg2;
}
}
$foo = new Foo();
call_user_func_array(array($foo, 'bar'), array(1, 2, 3, 4, 5));
Will output
将输出
1, 2
For dynamic arguments, use func_get_args like this :
对于动态参数,使用func_get_args如下:
class Foo {
public function bar() {
$args = func_get_args();
echo implode(', ', $args);
}
}
$foo = new Foo();
call_user_func_array(array($foo, 'bar'), array(1, 2, 3, 4, 5));
Will output
将输出
1, 2, 3, 4, 5