8

Say I got a class like:

<?
class ObjectModel {
}

and I got some other classes like:

<?
class SomeNewClass extends ObjectModel {
}

class SomeOtherNewClass extends ObjectModel {
}

Is there a way to get the children (SomeNewClass & SomeOtherNewClass) based on the ObjectModel class?

George Cummins
  • 28,485
  • 8
  • 71
  • 90
Andre Zimpel
  • 2,323
  • 4
  • 27
  • 42
  • No, you can't do this directly. Why do you need this? A reasonable way to do it is to keep track of this in a separate configuration file, but that's kinda tedious. The point of inheritance is to hide details, requesting the inheritance chain is in direct conflict with that goal. You shouldn't have to know. – Halcyon May 21 '13 at 21:47
  • You can register new children via `parent::__construct()` but that may be clunky. Can you clarify your intention? – George Cummins May 21 '13 at 21:50
  • I'm building a PHP-CMS. And every model in it inherits from the ObjectModel. Now, if a controller should look for a specific model, which could be at a dynamic path, it would be nice to look for that class and use it. e.g: the SomeNewClass model could be under /_components/sample/_models/somenewclass.php or it could be under /_brain/_components/navigation/_models/somenewclass.php – Andre Zimpel May 21 '13 at 21:52
  • 1
    You could get a list of all declared classes 'get_declared_classes()' and iterate through that list with 'instance_of' – Jordonias May 21 '13 at 21:55

3 Answers3

15
class ObjectModel {
}

class SomeNewClass extends ObjectModel {
}

class SomeOtherNewClass extends ObjectModel {
}

class SomeOtherNewClassLol extends ObjectModel {
}

function get_extends_number($base){
    $rt=0;
  foreach(get_declared_classes() as $class)
        if(is_subclass_of($class,$base)) $rt++;
        return $rt;
}

echo get_extends_number('ObjectModel'); //output: 3

Yes, you can do it, DEMO

Sam
  • 2,950
  • 1
  • 18
  • 26
  • 1
    If `SomeOtherNewClassLol` extends `SomeOtherNewClass` you'll get the same result. – Alix Axel May 21 '13 at 21:59
  • @AlixAxel If SomeOtherNewClassLol extends SomeOtherNewClass, you extend also ObjectModel, then his methods/proprieties not private can be used – Sam May 21 '13 at 22:02
  • @AlixAxel In that case, `SomeOtherNewClassLol` is still inheriting `ObjectModel`. It will still have access to methods/properties created in `ObjectModel` as it is still a descendant, even if a grandchild. – Jonathan Kuhn May 21 '13 at 22:04
  • 1
    Just saying that he asked for the class children (not grand-children), also his example code doesn't suggest he wants to go above 1 level of inheritance. – Alix Axel May 21 '13 at 22:04
  • Yep, level one would be enough. But the ability to get grand-children too is cool! – Andre Zimpel May 21 '13 at 22:07
4

You can iterate all classes returned by get_declared_classes() and inspecting their Reflection (Reflection::isSubclassOf)

But - this won't work when you are using autoloading.

jasir
  • 1,461
  • 11
  • 28
2

Not sure if this is going to get downvoted but you can hack your way through:

  1. get_declared_classes
  2. get_parent_class for each class on 1

It's not pretty, but if the child classes are loaded, it's possible.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500