简体   繁体   English

如何获取扩展PHP中特定类的类的名称

[英]How to get the name of the classes which extends a specific class in php

let I have a class named A. I have another two classes B and C. Both are extended from the class A. Now I want to get the name of the classes which is extended from class A ie B and C. 让我有一个名为A的类。我还有另外两个B和C类。这两个类都是从A类扩展的。现在,我想获取从A类扩展的类的名称,即B和C。

class A{
}
class B extends A{
}
class C extends A{
}

Now I want to get the name B and C. 现在我想获得名称B和C。

I have tried using instanceof 我尝试使用instanceof

$obj=new B();
if($obj instanceof A)
   echo "derived";

but to do so I have to know the class name. 但要做到这一点,我必须知道班级名称。

<?php
class A
{

}
class B extends A
{

}
class C extends A
{

}

$b = new B();

if ($b instanceof A) {
    var_dump(get_class($b)); // print B
    var_dump(get_parent_class($b)); // print A
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM