2

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:

  • MyTestPlugin
  • AnotherTestPlugin

abstract class Plugins
{
    // class properties and methods
}


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


class AnotherTestPlugin extends PLugins
{
    // class properties and methods
}
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • 1
    Maybe: http://stackoverflow.com/questions/6671398/get-all-defined-classes-of-a-parent-class-in-php or http://stackoverflow.com/questions/16680040/get-all-extended-classes-in-php – jeremy Feb 10 '16 at 01:47
  • @Jeremy thanks a mixture of the 2 got me the desired results! I will post it for others – JasonDavis Feb 10 '16 at 02:03

2 Answers2

1

Yes, you can do through Reflection:

$children  = array();
foreach( get_declared_classes() as $class )
{
    $reflected = new ReflectionClass( $class );
    if( $reflected->isSubclassOf( 'Plugins' ) ) $children[] = $class;
}
fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • I just posted a solution I came up with from the comments when you posted this answer. IT is similar but yours uses the ReflectionClass which is pretty cool! thanks – JasonDavis Feb 10 '16 at 02:06
  • I just realized I made a big mistake. I am trying to basically include the plugin files of all my plugins and then instantiate a new class for each plugin Class that is found. That is what I need the class names for but that also means that the classes will not be instantiated yet so they wont exist as a subclass yet! So I might need to parse the actual PHP file to get my names it seems – JasonDavis Feb 10 '16 at 02:09
  • Ahh I was wrong....turns out these still work even if the class is not instantiated, simply defining it makes it show up...perfect! – JasonDavis Feb 10 '16 at 02:12
  • @JasonDavis If you don't want load it all, you can use `token_get_all` (anyway you have to read file through `file_get_contents`). Years ago I had a similar problem, having to check before load to avoid names conflicts. – fusion3k Feb 10 '16 at 02:18
1
<?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>';
    }

}

?>
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • Probably this method is more performant that mine! +1 :) and the question is in my fav – fusion3k Feb 10 '16 at 02:08
  • @fusion3k maybe but I appreciate your answer...always nice to see different ways of doing things and the whole ReflectionCLass things is interesting since I have never had a use for it in 15 years until this! – JasonDavis Feb 10 '16 at 02:14
  • I've discovered it a few months ago! – fusion3k Feb 10 '16 at 02:19