Get all defined classes of a parent class in php

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

I need to determine, after all files have been included, which classes extend a parent class, so:

class foo{ } class boo extends foo{ } class bar extends foo{ } 

and I'd like to be able to grab an array like:

array('boo','bar'); 

回答1:

If you need that, it really smells like bad code, the base class shouldn't need to know this.

However, if you definitions have been included (i.e. you don't need to include new files with classes you possibly have), you could run:

$children  = array(); foreach(get_declared_classes() as $class){     if($class instanceof foo) $children[] = $class; } 


回答2:

Taking Wrikken's answer and correcting it using Scott BonAmi's suggestion and you get:

$children = array(); foreach( get_declared_classes() as $class ){   if( is_subclass_of( $class, 'foo' ) )     $children[] = $class; } 

The other suggestions of is_a() and instanceof don't work for this because both of them expect an instance of an object, not a classname.



回答3:

Use

$allClasses = get_declared_classes();

to get a list of all classes.

Then, use PHP's Reflection feature to build the inheritance tree.



回答4:

I am pretty sure that the following solution or some thing like that would be a good fit for your problem. IMHO, you can do the following (which is kind of observer pattern):

1- Define an interface call it Fooable

interface Fooable{     public function doSomething(); } 

2- All your target classes must implement that interface:

class Fooer implements Fooable{     public function doSomething(){          return "doing something";     } }  class SpecialFooer implements Fooable{     public function doSomething(){          return "doing something special";     } } 

3- Make a registrar class call it the FooRegisterar

class FooRegisterar{     public static $listOfFooers =array();      public static function register($name, Fooable $fooer){          self::$listOfFooers[$name]=$fooer;     }     public static function getRegisterdFooers(){          return self::$listOfFooers;     } } 

4- Somewhere in your boot script or some script that is included in the boot script:

FooRegisterar::register("Fooer",new Fooer()); FooRegisterar::register("Special Fooer",new SpecialFooer()); 

5- In your main code:

class FooClient{      public function fooSomething(){          $fooers = FooRegisterar::getRegisterdFooers();          foreach($fooers as $fooer){               $fooer->doSomthing();          }     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!