Objects\toArray()

transformer returns Closure pure
() → (object → map)
At a glance — No-argument constructor — just call toArray() and you get the Closure. Useful as a mapper across a list of objects to produce plain arrays.

Returns a Closure that converts any object into an associative array of its public properties. Non-public properties and methods are not included.

/**
  * @return Closure(object):array<string, mixed>
  */
Objects\toArray(): Closure

Returned Closure

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

/**
  * @param object $obj
  * @return array<string, mixed>
  */
$function (object $obj): array

Examples

Partial Application

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

$toArr = Objects\toArray();

$user = new class {
  public $name = 'Ada';
  public $role = 'admin';
  private $secret = 'hidden';
};

print_r($toArr($user)); // ['name' => 'Ada', 'role' => 'admin']
// $secret is private, so it's not included.

// Map a list of objects to arrays.
$rows = array_map(Objects\toArray(), [$user1, $user2, $user3]);

Details

Objects Functions

Releated Conversion Functions