如何在php中动态检查函数的参数个数

Tib*_*ibi 11 php

如何在运行时回家查看PHP中方法或函数的许多参数.

class foo {
   function bar ( arg1, arg2 ){
    .....
   }
}

我需要知道是否有办法运行类似的东西

get_func_arg_number ( "foo", "bar" )

结果是

2

Gre*_*reg 29

你需要使用反射来做到这一点.

$method = new ReflectionMethod('foo', 'bar');
$num = $method->getNumberOfParameters();
Run Code Online (Sandbox Code Playgroud)


Ala*_*orm 9

反思就是你在这里所追求的

class foo {
   function bar ( $arg1, $arg2 ){

   }
}
$ReflectionFoo = new ReflectionClass('foo');
echo $ReflectionFoo->getMethod('bar')->getNumberOfParameters();
Run Code Online (Sandbox Code Playgroud)


gnu*_*nud 2

您正在寻找 PHP5 中的反射功能 -此处的文档。

具体来说,请查看 ReflectionFunction 和 ReflcetionMethod 类。