Objects\isInstanceOf()

predicate returns Closure returns bool pure
(C | class-string) → ((C | class-string) → bool)
At a glance — Bind a class up front; the returned Closure answers "is this an instance of it?" for any object or class-string passed in.

Creates a predicate Closure that is true when the passed class or object is an instance of the bound class.

/**
  * @param object|class-string $class
  * @return Closure(object|class-string):bool
  */
Objects\isInstanceOf($class): Closure

Returned Closure

When Objects\isInstanceOf() 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.

$isException = Objects\isInstanceOf(\Exception::class);

var_dump($isException(new \RuntimeException()));  // true
var_dump($isException(new \stdClass()));          // false

// As a filter predicate over a mixed list.
$exceptions = array_filter(
  [new \RuntimeException(), new \stdClass(), new \LogicException()],
  $isException
);

count($exceptions); // 2

Curried

This can be called inline using currying.

var_dump(Objects\isInstanceOf(\Countable::class)(new \ArrayObject())); // true

Details

Objects Functions

Releated Type check Functions