GeneralFunctions\hasProperty()

predicate returns Closure returns bool pure
string → (Record → bool)
At a glance — Existence check — cares whether the key is set, not whether the value is truthy.

Creates a predicate Closure that is true when a given property/index exists on an array or object.

/**
  * @param string $property
  * @return Closure(mixed[]|object):bool
  */
GeneralFunctions\hasProperty(string $property): Closure

Returned Closure

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

/**
  * @param array|object $record
  * @return bool
  */
$function ($record): bool

Examples

Partial Application

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

$hasEmail = GeneralFunctions\hasProperty('email');

$users = [
  ['name' => 'Ada', 'email' => 'a@x'],
  ['name' => 'Bea'],
];

$withEmail = array_filter($users, $hasEmail);
print_r($withEmail); // [['name' => 'Ada', 'email' => 'a@x']]

Details

General Functions

Releated Property access Functions