Objects\usesTrait()

predicate returns Closure returns bool pure
(string, bool) → (object → bool)
At a glance — Trait-based counterpart to implementsInterface. Bind a trait up front; the returned Closure tests any object for its presence.

Creates a predicate Closure that is true when the passed object (or its class hierarchy) uses the bound trait.

/**
  * @param string $trait Trait class-string.
  * @param bool $autoload Whether to call __autoload by default.
  * @return Closure(object):bool
  */
Objects\usesTrait(string $trait, bool $autoload = true): Closure

Returned Closure

When Objects\usesTrait() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param object $obj
  * @return bool
  */
$function (object $obj): bool

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

$hasLoggerTrait = Objects\usesTrait(LoggerAware::class);

var_dump($hasLoggerTrait($service));  // true/false depending on the service class

// Filter a list of services down to just those with logging.
$loggable = array_filter($services, $hasLoggerTrait);

Details

Objects Functions

Releated Type check Functions