我正在尝试输入$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 5和7中支持的->getClass()
.
<?php
class MyClass {
}
class Foo
{
public function test(stdClass $bar)
{
}
public function another_test(array $arr) {
}
public function final_test(MyClass $var) {
}
}
$reflect = new ReflectionClass('Foo');
foreach ($reflect->getMethods() as $method) {
foreach ($method->getParameters() as $num => $parameter) {
var_dump($parameter->getClass());
}
}
我说类的原因是因为在数组上,它将返回NULL.
object(ReflectionClass)#6 (1) {
["name"]=>
string(8) "stdClass"
}
NULL
object(ReflectionClass)#6 (1) {
["name"]=>
string(7) "MyClass"
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。