2
我想获得类内部的方法列表以及它们的参数和默认值。我怎样才能做到这一点?下面是我使用的代码:php获取方法参数的默认值
$class = new ReflectionClass($className);
$methods = [];
foreach($class->getMethods() as $method){
if($method->class == $className && $method->name != '__construct'){
$obj = [];
$obj['controller'] = $className;
$obj['action'] = $method->name;
$obj['params'] = array_map(function($value){return $value->name;}, $method->getParameters());
$methods[] = $obj;
}
}
以上代码的样本结果是这样的:
Array(
[0] => Array
(
[controller] => Controller,
[action] => function,
[params] => Array
(
[0] => offset,
[1] => limit
)
)
)
我怎样才能得到函数参数的默认值?
您的标题应该是更具体,写得很好。因为当我看到标题 – alamin
时,我误以为它是另一篇文章。通过使用'getParameters()'获得ReflectionParameter对象的列表。您可以使用类'[getDefaultValue](http://php.net/manual/en/reflectionparameter.getdefaultvalue.php)方法从这些对象中获取默认值。 –
IDK,如果这是您要查找的内容,但他可能会有所帮助:http://geneticcoder.blogspot.com/2015/05/logging-class-usage-in-php.html –