Creating a strictly typed collection of objects in PHP

Daniel Opitz
Daniel Opitz
30 Aug 2019

Attention: This article has been updated. Here is the new link: Collections in PHP

Table of contents

Intro

A type-checked array is an array that ensures that all values of an array are an instance of the same data type (class).

Until today it is not possible directly with arrays and I think this is a missing feature in PHP.

There is an interesting RFC about Generic arrays, but whether and when this feature will be added is still unclear.

However, you can also implement it with the existing language features.

Creating a collection class

First, we create a class we want to add to the collection:

<?php

final class User
{
}

Now we create a collection class to collect and retrieve our “array” of user objects.

Example:

final class UserList
{
    /**
     * @var User[] The users
     */
    private array $list;

    /**
     * The constructor.
     * 
     * @param User ...$user The users
     */
    public function __construct(User ...$user) 
    {
        $this->list = $user;
    }
    
    /**
     * Add user to list.
     *
     * @param User $user The user
     *
     * @return void
     */
    public function add(User $user): void
    {
        $this->list[] = $user;
    }

    /**
     * Get all users.
     *
     * @return User[] The users
     */
    public function all(): array
    {
        return $this->list;
    }
}

Usage

// Create a new (and empty) collection object
$userList = new UserList();

// Add some new User objects to the collection
$userList->add(new User());
$userList->add(new User());

Iterating over the collection:

foreach ($userList->all() as $user) {
    // ...
}

You can also use variadics and pass any number of arguments into it:

$userList = new UserList(new User(), new User());

Passing an array of Users to the class constructor:

$userList = new UserList(...[new User(), new User()]);

Demo