0
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);
}

Want returned value to simply be 'Exception' and 'array' not "Exception $a" and "array $id". What should the regex be. Working on php 5.3

1 Answer 1

1

Just use the Reflection classes

foreach( (new ReflectionMethod('C', 'methodA'))->getParameters() as $param)
{
    $class = $param->getClass();
    if ($class) {
        echo $class->getName()."\n";
    } elseif ($param->isArray()) {
        echo "array\n";
    }  else {
        echo "unknown\n";
    }
}
3
  • Tried that but returns null if hint is Array Commented Oct 1, 2015 at 16:19
  • Ok it should be fine now
    – Alfwed
    Commented Oct 1, 2015 at 16:23
  • That should do the trick. I should have thought of that. Cheers Commented Oct 1, 2015 at 16:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.