【发布时间】:2017-04-06 14:26:08
【问题描述】:
我想通过ReflectionClass获取一个类属性的命名空间。
namespace Foo\Bar;
use Foo\Quux\B;
class A{
/**
* @var B $b
*/
protected $b = null;
...
}
在另一个类中,我正在创建一个 ReflectionClass 对象并尝试获取属性 b 的命名空间,以便我可以为它的类创建 ReflectionClass。
$namespace = "Foo\\Bar";
$classname = "A";
$rc = new \ReflectionClass("$namespace\\$classname");
$type = getVarType($rc->getProperty("b")->getDocComment()); // returns B
$ns = $rc->getProperty("b")-> ... // I want to find out the namespace of class B
$rc1 = new \ReflectionClass("$ns\\$type");
是否有可能找出类 A 使用了哪些命名空间?
然后我可以将class A 使用的命名空间与发现的属性类型配对。
我知道我可以用这样的类型提示来解决这个问题:@var Foo\Quux\B $b,但是我有很多像 class A 这样的类,有很多属性和方法使用像 b 这样的类型,我不想重构整个代码。
是否可以从use 语句中找到命名空间,或者我必须使用显式类型提示?
谢谢!
【问题讨论】:
标签: php reflection namespaces php-5.6