class C
{
function methodA(Exception $a, array $id){}
}
function getClassName(ReflectionParameter $param) {
$regex = '/[([^]]*)]/';
preg_match($regex, $param->__toString(), $matches);
return isset($matches[1]) ? $matches[1] : null;
}
foreach( new ReflectionMethod('C', 'methodA')->getParameters() as $param)
{
echo getClassName($param);
}
希望返回值只是"Exception"one_answers"array",而不是"Exception $a"one_answers"array $id"。正则表达式应该是什么?
直接使用Reflection类
foreach( (new ReflectionMethod('C', 'methodA'))->getParameters() as $param)
{
$class = $param->getClass();
if ($class) {
echo $class->getName()."n";
} elseif ($param->isArray()) {
echo "arrayn";
} else {
echo "unknownn";
}
}