In PHP, how do I map objects with PHP 8+ features?

In PHP 8 and later, you can leverage features such as the nullsafe operator, attributes, and union types to effectively map objects. One common approach is to utilize the `array_map` function in conjunction with closure and objects.

PHP 8, Object Mapping, Nullsafe Operator, Attributes, Union Types
This example demonstrates how to map an array of user objects to their respective names using modern PHP features.
<?php class User { public function __construct( public string $name, public int $age, ) {} } $users = [ new User("Alice", 30), new User("Bob", 25), new User("Charlie", 35), ]; $names = array_map(fn($user) => $user->name, $users); print_r($names); // Output: Array ( [0] => Alice [1] => Bob [2] => Charlie ) ?>

PHP 8 Object Mapping Nullsafe Operator Attributes Union Types