加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 获取类函数的参数数量

发布时间:2020-12-13 16:00:56 所属栏目:PHP教程 来源:网络整理
导读:有没有办法检测类中函数的参数数量? 我想做的是以下内容. $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) ==
有没有办法检测类中函数的参数数量?

我想做的是以下内容.

$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);
}

这可能吗?

使用反射,但这实际上是代码中的开销;并且方法可以具有任意数量的参数,而不在方法定义中明确定义它们.
$classMethod = new ReflectionMethod($class,$method);
$argumentCount = count($classMethod->getParameters());

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读