Objects\implementsInterface()

predicate returns Closure returns bool pure
(class-string | object) → ((class-string | object) → bool)
At a glance — Like 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): Closure

Returned Closure

When 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): bool

Examples

Partial Application

This 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)
);

Details

Objects Functions

Releated Type check Functions