如何知道另一个类的方法中需要多少参数?PHP
例如: 文件:\bla\test1.php class Test { public function Hello($one) { } } 课堂测试{ 公共功能你好($1){ } } 文件:\bla2\test2.php class World { public function GetNumberOfParameters($class, $method) { // output: number of parameters required of method Hello } } 阶级世界{ 公共函数GetNumberOfParameters($class,$method){ //输出:方法Hello所需的参数数 } }如何知道另一个类的方法中需要多少参数?PHP,php,Php,例如: 文件:\bla\test1.php class Test { public function Hello($one) { } } 课堂测试{ 公共功能你好($1){ } } 文件:\bla2\test2.php class World { public function GetNumberOfParameters($class, $method) { // output: number of parameters required of met
您可以使用反射来获取参数的数量。比如:
$reflec = new ReflectionClass('Test');
$helloMethod = $reflec->getMethod('Hello');
echo "Number of params is: ".$helloMethod->getNumberOfParameters();
请看这里:
我会考虑你为什么要这么做。我能想到的只有极少数情况下这会有任何用处
示例:我不确定(没有很好的文档记录),但请查看和。(这些评论可能会有帮助。)谢谢!但是这个解决方案不起作用。。这给了我一个错误“类不存在”@user5011503,这意味着该类尚未加载。您是否包含或定义了它?看这个例子