我怎么知道另一个类的方法中需要多少个参数?.PHP


How i can know how many parameters are necessary in a method of another class? PHP

示例:

文件: ''bla''test1.php

类测试 {    公共函数 Hello($one) {    }}

文件: ''bla2''test2.php

类世界 {    public function GetNumberOfParameters($class, $method) {       输出:方法所需的参数数 你好    }}

您可以使用反射来获取参数的数量。像这样:

$reflec = new ReflectionClass('Test');
$helloMethod = $reflec->getMethod('Hello');
echo "Number of params is: ".$helloMethod->getNumberOfParameters();

看这里: http://php.net/manual/en/class.reflectionclass.php

但是,我会考虑您为什么要这样做。我能想到很少有这种情况会有任何用处。

示例:http://codepad.org/zWca7mFz