PHP: how to get a list of classes that implement certain interface?

后端 未结 4 1235
無奈伤痛
無奈伤痛 2020-12-04 23:37

I\'ve got an interface

interface IModule {
    public function Install();
}

and some classes that implement this interface

         


        
4条回答
  •  萌比男神i
    2020-12-05 00:36

    You can use PHP's ReflectionClass::implementsInterface and get_declared_classes functions to accomplish this:

    $classes = get_declared_classes();
    $implementsIModule = array();
    foreach($classes as $klass) {
       $reflect = new ReflectionClass($klass);
       if($reflect->implementsInterface('IModule')) 
          $implementsIModule[] = $klass;
    }
    

提交回复
热议问题