In PHP, how do I reduce objects with strong typing?

In PHP, you can reduce objects by using strong typing, which helps ensure that the types of the values you are working with are what you expect. Here's an example of how to reduce an array of objects to a single result using PHP's strong typing.

Example of reducing objects with strong typing:

<?php class Item { public string $name; public float $price; public function __construct(string $name, float $price) { $this->name = $name; $this->price = $price; } } function reduceItems(array $items): float { return array_reduce($items, function (float $carry, Item $item) { return $carry + $item->price; }, 0); } $items = [ new Item("Item 1", 10.5), new Item("Item 2", 20.75), new Item("Item 3", 5.25) ]; $totalPrice = reduceItems($items); echo "Total Price: " . $totalPrice; ?>

PHP Strong Typing Reduce Objects Array Reduce PHP Objects