JasonDavis
JasonDavis

Reputation: 48933

Get PHP Class names of classes that extend a class?

I have a PHP Class named Plugins in which I have other plguin style PHP Classes which extend from the PLugins class.

Is there a way to get all the PHP CLass Names that extend from the Plugins class? Perhaps using something like Reflection in PHP

So in my example below, I would be able to get the values:


abstract class Plugins
{
    // class properties and methods
}


class MyTestPlugin extends PLugins
{
    // class properties and methods
}


class AnotherTestPlugin extends PLugins
{
    // class properties and methods
}

Upvotes: 2

Views: 718

Answers (2)

JasonDavis
JasonDavis

Reputation: 48933

<?php

abstract class Plugins
{
    // class properties and methods
}


class MyTestPlugin extends Plugins
{
    // class properties and methods
}


class AnotherTestPlugin extends Plugins
{
    // class properties and methods
}

$plugin1 = new MyTestPlugin();
$plugin2 = new AnotherTestPlugin();


$parentClassName = 'Plugins';

foreach(get_declared_classes() as $class){
    if(is_subclass_of($class, $parentClassName)){
        echo $class.' == is a child class of '.$parentClassName.'<br>';
    }

}

?>

Upvotes: 1

fusion3k
fusion3k

Reputation: 11689

Yes, you can do through Reflection:

$children  = array();
foreach( get_declared_classes() as $class )
{
    $reflected = new ReflectionClass( $class );
    if( $reflected->isSubclassOf( 'Plugins' ) ) $children[] = $class;
}

Upvotes: 1

Related Questions