how to obtain all subclasses of a class in php

Viewed 10250

Is it possible to get all subclasses of given class in php?

3 Answers

Using PHP 7.4:

$children = array_filter(get_declared_classes(), fn($class) => is_subclass_of($class, MyClass::class)); 
function getClassNames(string $className): array
{
    $ref = new ReflectionClass($className);
    $parentRef = $ref->getParentClass();

    return array_unique(array_merge(
        [$className],
        $ref->getInterfaceNames(),
        $ref->getTraitNames(),
        $parentRef ?getClassNames($parentRef->getName()) : []
    ));
}
Related