GeneralFunctions\setProperty()

transformer returns Closure
(Record, string) → (mixed → Record)
At a glance — Curried setter. Bind the record + property up front, then feed values in. Mutates the target when an object is passed by reference.

Creates a Closure that writes a value into a named property/index on a record. Works with both arrays and objects.

/**
  * @param array<string,mixed>|ArrayObject<string,mixed>|object $store
  * @param string $property
  * @return Closure(mixed):(array<string,mixed>|ArrayObject<string,mixed>|object)
  */
GeneralFunctions\setProperty($store, string $property): Closure

Returned Closure

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

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

Examples

Partial Application

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

$user = (object) ['name' => 'Ada', 'role' => 'user'];

$setRole = GeneralFunctions\setProperty($user, 'role');

$promoted = $setRole('admin');
echo $promoted->role; // 'admin'

Details

General Functions

Releated Property access Functions