/**
* Get classes which implement a given interface
* @param string $interface_name Name of the interface
* @return array Array of names of classes. Empty array means input is a valid interface which no class is implementing. NULL means input is not even a valid interface name.
*/
function whoImplements($interface_name) {
if (interface_exists($interface_name)) {
return array_filter(get_declared_classes(), create_function('$className', "return in_array(\"$interface_name\", class_implements(\"\$className\"));"));
}
else {
return null;
}
}
示例调用的输出
var_export(whoImplements('ArrayAccess'));
将如下:
[sandbox]$ php whoimplementswhat.php
Array
(
[29] => CachingIterator
[30] => RecursiveCachingIterator
[38] => ArrayObject
[39] => ArrayIterator
[40] => RecursiveArrayIterator
[48] => SplDoublyLinkedList
[49] => SplQueue
[50] => SplStack
[55] => SplFixedArray
[56] => SplObjectStorage
[111] => Phar
[112] => PharData
)
这样,就不用循环了,可以在较低版本的PHP上运行代码。功能
array_filter