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

phpinterfacecertainimplementclasseslist

Last Update : 2023-09-22 UTC 11:31:05 AM

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

Example 1 - Echo all classes implementing the w3coded interface certain Iterator Interface,Example 2 - Return array of w3coded interface certain all classes implementing the Iterator w3coded interface certain Interface,You can use PHP's w3coded interface certain ReflectionClass::implementsInterface and w3coded interface certain get_declared_classes functions to accomplish w3coded interface certain this:,class_implements — Return the interfaces w3coded interface certain which are implemented by the given class

Usage

in_array('InterfaceName', class_implements('className'));

Example 1 - Echo all classes implementing the Iterator Interface

foreach (get_declared_classes() as $className) {
    if (in_array('Iterator', class_implements($className))) {
        echo $className, PHP_EOL;
    }
}

Example 2 - Return array of all classes implementing the Iterator Interface

print_r(
    array_filter(
        get_declared_classes(), 
        function ($className) {
            return in_array('Iterator', class_implements($className));
        }
    )
);

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;
}

Generic solution:

function getImplementingClasses( $interfaceName ) {
    return array_filter(
        get_declared_classes(),
        function( $className ) use ( $interfaceName ) {
            return in_array( $interfaceName, class_implements( $className ) );
        }
    );
}

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

Newly Added Questions

Similar Questions

Questions :

How To Group Array Key Value

Last Update : 2023-09-22 UTC 13:29:19 PM

Questions :

PhpStorm Warning For React Attributes In Jsx File With SCSS File

Last Update : 2023-09-22 UTC 13:29:03 PM

Questions :

Why Is The File Not Showing Up In Request.files And In Request.forms Instead?

Last Update : 2023-09-22 UTC 13:28:51 PM

Questions :

Proxying Assets From React App Directory In Slim Framework?

Last Update : 2023-09-22 UTC 13:28:35 PM

Questions :

Laravel 5.4 Can't Run “php Artisan Preset React” Comand

Last Update : 2023-09-22 UTC 13:28:15 PM

Questions :

How To Update Session Values Without Signing Out?

Last Update : 2023-09-22 UTC 13:27:59 PM

Questions :

Array Is Not Visible

Last Update : 2023-09-22 UTC 13:27:53 PM

Questions :

React Routing For Login Using Symfony

Last Update : 2023-09-22 UTC 13:27:48 PM

Questions :

Sanctum With React SPA Returning 419 Page Expired

Last Update : 2023-09-22 UTC 13:27:37 PM

Questions :

How Do I Import An Input String Into Another Page

Last Update : 2023-09-22 UTC 13:27:17 PM

Top
© 2023 W3CODED - All Rights Reserved.