假设我上了一节课,比如:

<?
class ObjectModel {
}

我还上了其他课程,比如:
<?
class SomeNewClass extends ObjectModel {
}

class SomeOtherNewClass extends ObjectModel {
}

是否有方法基于objectmodel类获取子类(somenewclass&someothernewclass)?

最佳答案

class ObjectModel {
}

class SomeNewClass extends ObjectModel {
}

class SomeOtherNewClass extends ObjectModel {
}

class SomeOtherNewClassLol extends ObjectModel {
}

function get_extends_number($base){
    $rt=0;
  foreach(get_declared_classes() as $class)
        if(is_subclass_of($class,$base)) $rt++;
        return $rt;
}

echo get_extends_number('ObjectModel'); //output: 3

是的,你可以做到,DEMO

08-26 08:18