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

In PHP, to index objects with strong typing, you can use concrete classes and type hints. The following example demonstrates how to accomplish this:

<?php class Person { public string $name; public int $age; public function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } } class PeopleIndex { private array $people = []; public function addPerson(Person $person): void { $this->people[] = $person; } public function getPeople(): array { return $this->people; } } $index = new PeopleIndex(); $index->addPerson(new Person("Alice", 30)); $index->addPerson(new Person("Bob", 25)); foreach ($index->getPeople() as $person) { echo $person->name . " is " . $person->age . " years old.\n"; } ?>

PHP Strong Typing Object Indexing Type Hinting PHP Objects