开发者

how to obtain all subclasses of a class in php

开发者 https://www.devze.com 2023-01-11 02:19 出处:网络
Is it possible to get all subclas开发者_Python百科ses of given class in php?function getSubclassesOf($parent) {

Is it possible to get all subclas开发者_Python百科ses of given class in php?


function getSubclassesOf($parent) {
    $result = array();
    foreach (get_declared_classes() as $class) {
        if (is_subclass_of($class, $parent))
            $result[] = $class;
    }
    return $result;
}

Coincidentally, this implementation is exactly the one given in the question linked to by Vadim.


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()) : []
    ));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消