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

You dont need Reflection for this. You can simply use

  • class_implements — Return the interfaces 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));
        }
    )
);

The second example requires PHP5.3 due to the callback being an anonymous function.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)