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(): ClosureWhen 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): arrayThis 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]);