Arrays\toObject()

transformer terminal accepts iterable returns Closure throws
object? → (Iterable → object)
At a glance — Terminal — consumes the whole source. Throws if a target property does not exist or is not public.

Creates a Closure that casts an array or iterable into an object — assigns each key/value pair as a property on the target. Defaults to stdClass when no target is supplied.

/**
  * @param object|null $object Target object; null = stdClass.
  * @return Closure(iterable<int|string, mixed>):object
  * @throws \InvalidArgumentException If a property does not exist or is not public.
  */
Arrays\toObject($object = null): Closure

Returned Closure

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

/**
  * @param iterable<int|string, mixed> $source
  * @return object
  */
$function (iterable $source): object

Examples

Partial Application

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

$toStd = Arrays\toObject();
$obj = $toStd(['name' => 'Ada', 'age' => 42]);

echo $obj->name; // Ada

Details

Arrays Functions

Releated Array cast Functions