如何在PHP中使用Reflection类获取扩展接口
我有一个实现接口的类,并且该接口扩展了另一个接口。设置是这样的:
interface A{
}
interface B extends A {
}
class C implements B {
}
$obj = new C();
我想知道对象$obj实现了哪些接口。我尝试创建一个 ReflectionClass 对象,然后调用 getInterfaces 方法,但它只返回接口 B:
$reflection = new ReflectionClass($obj);
print_r($reflection->getInterfaces());
我还尝试使用接口名称创建一个 ReflectionClass 对象,但当我调用 getInterfaces() 方法时,它返回一个空大批。
你们中有人知道如何获取扩展给定接口的接口名称吗?
非常感谢您的帮助, 史蒂夫
I have a class that implements an interface, and that interface extends another interface. The setting is like that:
interface A{
}
interface B extends A {
}
class C implements B {
}
$obj = new C();
I want to know which interfaces the object $obj implements. I tried to create a ReflectionClass object and then calling the getInterfaces method, but it only returns me the interface B:
$reflection = new ReflectionClass($obj);
print_r($reflection->getInterfaces());
I also tried to create a ReflectionClass object using the interface name, but when I call the getInterfaces() method, it returns an empty array.
Does any of you guys know how to get an interface name that extends a given interface?
Thanks a lot for your help,
Steve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要为此进行反射。您可以简单地使用
class_implements
— 返回给定类示例 你的代码片段:输出:
You do not need Reflection for this. You can simply use
class_implements
— Return the interfaces which are implemented by the given classExample for your code snippet:
Output:
您的示例为我输出以下内容(IDE 调试器在 PHP 5.2.17 上运行):
Your example outputs this for me (IDE debugger is running on PHP 5.2.17):