I need to get all declared classes that have extended another parent class.
So for example ...
class ParentClass { } class ChildOne extends ParentClass { } class ChildTwo extends ParentClass { } class ChildThree { }
I need an array that outputs this:
array('ChildOne', 'ChildTwo')
I am new to PHP OOP, but based on some Googling, I came up with this solution.
$classes = array(); foreach( get_declared_classes() as $class ) { if ( is_subclass_of($class, 'ParentClass') ){ array_push($classes, $class); } }
I want to ask if this is the best practice to do what I want to do, or is there a better way? The global scope will contain many other classes that are not children of the ParentClass . Is looping all declared classes the best way?
EDIT (goal clarification):
I want to achieve this in order to instantiate each child class extending the parent class.
I want to do $childone = new ChildOne; $childtwo = new ChildTwo; $childone = new ChildOne; $childtwo = new ChildTwo; for each child of the ParentClass .
source share