我正在尝试获取类型$bar
变量。
<?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());
}
}
我希望,stdClass
但是我得到
Call to undefined method ReflectionParameter::getType()
有什么事吗 还是有其他方法?
$ 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它也应适用于数组类型。
看起来已经在PHP Reflection中添加了类似的问题-将方法参数类型作为字符串获取
我编写了适用于所有情况的解决方案:
/**
* @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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句