我想通过ReflectionMethod:访问这些方法
$r = new ReflectionMethod($class, $method);
$params = $r->getParameters();
如果需要参数($Option),则获取该方法的来源以准备参数:
public function GetFeedback($Options) {
$this->checkConnection();
return $this->_client->doGetFeedback(
$Options['feedback-from'],
$Options['feedback-to']
);
}
所以在我的例子中,要找到$Option键-"feedback from"answers"feedbacking to"
假设一个类像
class Foo
{
public function GetFeedback($Options) {
$this->checkConnection();
return $this->_client->doGetFeedback(
$Options['feedback-from'],
$Options['feedback-to']
);
}
}
这个代码可以满足的要求
$reflector = new ReflectionMethod('Foo', 'GetFeedback');
$methodBody = implode(
'',
iterator_to_array(
new LimitIterator(
new SplFileObject($reflector->getFileName()),
$reflector->getStartLine(),
$reflector->getEndLine() - $reflector->getStartLine()
)
)
);
foreach ($reflector->getParameters() as $parameter) {
if (!$parameter->isOptional()) {
preg_match_all(
sprintf('{'$%s'[[''"](.*)[''"]']}', $parameter->getName()),
$methodBody,
$matches
);
}
print_r($matches);
}
输出:
Array
(
[0] => Array
(
[0] => $Options['feedback-from']
[1] => $Options['feedback-to']
)
[1] => Array
(
[0] => feedback-from
[1] => feedback-to
)
)
然而,这种方法IMO的唯一有效用法是生成一个列出这些选项的文档块,并同意如果您在生产代码中需要这样做,您可能会做错。