获取类函数的参数数量 [英] Get number of arguments for a class function

查看:100
本文介绍了获取类函数的参数数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有什么方法可以检测一个类中函数的参数数量?我想要做的是以下内容。

  $ class ='foo'; 
$ path ='path / to / file';
if(!file_exists($ path)){
die();
}

require($ path); $!
$ b if(!class_exists($ class)){
die();
}

$ c =新班;
$ b $ if(num_function_args($ class,$ function)== count($ url_segments)){
$ c-> $ function($ one,$ two,$ three);
}

这可能吗?

解决方案

使用反射,但这实际上是代码中的开销;并且一个方法可以包含任何数量的参数,而不必在方法定义中明确定义它们。

  $ classMethod = new ReflectionMethod($类,$方法); 
$ argumentCount = count($ classMethod-> getParameters());


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?

解决方案

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());

这篇关于获取类函数的参数数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆