Reputation: 48933
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
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
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