I'm trying to get type $bar
variable.
<?php
class Foo
{
public function test(stdClass $bar, array $foo)
{
}
}
$reflect = new ReflectionClass('Foo');
foreach ($reflect->getMethods() as $method) {
foreach ($method->getParameters() as $num => $parameter) {
var_dump($parameter->getType());
}
}
I expect stdClass
but I get
Call to undefined method ReflectionParameter::getType()
What can be wrong? Or there is some another way?..
$ php -v
PHP 5.4.41 (cli) (built: May 14 2015 02:34:29)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
UPD1 It should work for the array type as well.
It looks like similar question already added in PHP Reflection - Get Method Parameter Type As String
I wrote my solution it works for all cases:
/**
* @param ReflectionParameter $parameter
* @return string|null
*/
function getParameterType(ReflectionParameter $parameter)
{
$export = ReflectionParameter::export(
array(
$parameter->getDeclaringClass()->name,
$parameter->getDeclaringFunction()->name
),
$parameter->name,
true
);
return preg_match('/[>] ([A-z]+) /', $export, $matches)
? $matches[1] : null;
}