iTranslated by AI
[PHP] Creating an iterable First-Class Collection with runtime type safety for each element
Introduction
Previously, I wrote an article like this. Since PHP cannot restrict the types of elements within an array, it explained how you can create first-class collections using variadic arguments.
If the iteration logic is encapsulated within the collection, there is no strict need for it to be compatible with foreach. However, I decided to explore what would happen if I tried to make it possible.
Issue: IDE autocompletion doesn't work when iterating with foreach
You can easily use an Iterator using the methods introduced in articles like the one below.
However, there is an issue where the IDE cannot resolve the type of each element retrieved when looping with foreach. Below is an example of implementing a collection Greeters for the Greeter class using IteratorAggregate, as shown in the article above.
The Greeter class implements a greeting method, but since the IDE cannot resolve the type, autocompletion does not work. (This is because the return value type hint, interpreted at runtime, is mixed.)

That said, it is possible to inform the IDE of the type by adding an assert inside the foreach or by adding comments that static analysis tools can interpret.

What I will introduce below is a way to ensure that a TypeError is thrown even at runtime if the type happens to be different.
How to do it
By implementing Iterator and specifying a type hint for the return value of the current method, you can solve the aforementioned issue.
Specifically, the implementation looks like this:
/**
* Abstract class implementing Iterator.
* Since we want to specify the type hint for the current method in the child class, we'll keep it as an abstract method.
* However, since the concrete implementation of the method is common to all child classes, we'll create a _current method.
*/
abstract class Collection implements \Iterator
{
private int $position = 0;
public function __construct(private array $values)
{
}
abstract public function current(): mixed;
public function _current(): mixed
{
return $this->values[$this->position];
}
public function next(): void
{
$this->position++;
}
public function key(): int
{
return $this->position;
}
public function valid(): bool
{
return isset($this->values[$this->position]);
}
public function rewind(): void
{
$this->position = 0;
}
}
/**
* Sample collection class. The constructor accepts variadic arguments to restrict the types within the array.
* Implement the current method and add a return type hint. Delegate the implementation logic to _current.
*/
class Greeters extends Collection
{
public function __construct(Greeter ...$values)
{
parent::__construct($values);
}
public function current(): Greeter
{
return $this->_current();
}
}
/**
* Sample class for which we want to create a collection this time
*/
class Greeter
{
public function greeting(): void
{
echo 'hello';
}
}
Autocompletion now works correctly.

Conclusion
Despite having introduced this, I personally thought, "Maybe it's better to just ensure this in CI using something like Psalm without doing all this tedious work."
Let's make friends with static analysis tools!
Discussion