GeneralFunctions\getProperty()

transformer returns Closure pure
string → (Record → mixed)
At a glance — One getter works on both arrays and objects. Returns null if the property is missing.

Creates a Closure that reads a named property/index from any array or object. Works transparently across both — `['name' => 'Ada']` and `(object)['name' => 'Ada']` both yield 'Ada'.

/**
  * @param string $property
  * @return Closure(mixed):mixed
  */
GeneralFunctions\getProperty(string $property): Closure

Returned Closure

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

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

Examples

Partial Application

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

$getName = GeneralFunctions\getProperty('name');

echo $getName(['name' => 'Ada']);  // 'Ada'
echo $getName((object) ['name' => 'Bea']); // 'Bea'

// As a callback:
$names = array_map($getName, [
  ['name' => 'Ada'],
  ['name' => 'Bea'],
]);

print_r($names); // ['Ada', 'Bea']

Details

General Functions

Releated Property access Functions