AndreZimpel AndreZimpel - 4 months ago 18
PHP Question

Get all extended Classes in PHP

Say I got a class like:

<?
class ObjectModel {
}


and I got some other classes like:

<?
class SomeNewClass extends ObjectModel {
}

class SomeOtherNewClass extends ObjectModel {
}


Is there a way to get the children (SomeNewClass & SomeOtherNewClass) based on the ObjectModel class?

Sam Sam
Answer
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

Yes, you can do it, DEMO