isInstanceOf but specifically for interfaces. Works with either an object or a class-string.
Creates a predicate Closure that is true when the passed object or class-string implements the bound interface.
/**
* @param object|class-string $interface
* @return Closure(object|class-string):bool
*/
Objects\implementsInterface($interface): ClosureWhen Objects\implementsInterface() is called, it returns the following Closure which can be used like a regular function.
/**
* @param object|class-string $target
* @return bool
*/
$function ($target): boolThis can be used to create a simple closure which can be used as a regular function.
$isCountable = Objects\implementsInterface(\Countable::class);
var_dump($isCountable(new \ArrayObject())); // true
var_dump($isCountable(new \stdClass())); // false
// Filter a mixed list down to just the Iterator-implementing instances.
$iterables = array_filter(
$mixed,
Objects\implementsInterface(\Iterator::class)
);