Table of Contents

A Gentle Breakdown of InMemoryUserRepository

Return to the original page
Here is the code we're unpacking:

class InMemoryUserRepository implements UserRepository {
    private array $items = [];
 
    public function find(int $id): ?User {
        return $this->items[$id] ?? null;
    }
 
    public function save(User $user): void {
        $this->items[$user->id] = $user;
    }
 
    public function all(): UserCollection {
        $collection = new UserCollection();
 
        foreach ($this->items as $user) {
            $collection->add($user);
        }
 
        return $collection;
    }
}

This class is a simple, in‑memory repository — a small teaching tool that shows the shape of a repository without involving databases or frameworks.

Let's slow down and explain every part.


1. class InMemoryUserRepository

implements UserRepository

class InMemoryUserRepository implements UserRepository {

This line introduces two ideas:

class InMemoryUserRepository
We're defining a class named InMemoryUserRepository.

Think of it as: “a filing cabinet that stores User entities in memory”.

implements UserRepository

This means:

“This class promises to follow the rules defined by the UserRepository interface”.
(described on section #3 in the original page).

An interface is like a contract.
It says:

The repository class agrees to provide those methods.


2. private array $items = [];

private array $items = [];

This line defines the internal storage.

private
Only this class can access $items.
No one outside the repository can touch it.

This protects the storage from accidental misuse.

array
The type: this property must always be an array.

$items
The name of the internal storage.

= [];
Initialize it as an empty array.

So the whole line means:

“Inside this repository, keep a private array named $items, starting empty”.

This is the in‑memory “filing cabinet”.


3. public function find(int $id): ?User { ... }

public function find(int $id): ?User {...}

This method retrieves a user by ID.

Let's break down the signature:

public
Anyone can call this method.

function find(…)
The method is named find — meaning “look up a user.”

int $id
The method expects an integer ID.

: ?User
This return type is either:

The ? means “nullable”.

So the method means:
“Give me the user with this ID, or null if none exists”.

Inside the method:

return $this->items[$id] ?? null;

Breakdown:

This is PHP's null‑coalescing operator.


4. public function save(User $user): void { ... }

public function save(User $user): void {...}

This method stores a user in the repository.

public
Anyone can call it.

User $user
The method requires a User object.

: void
It doesn’t return anything.

Inside the method:

$this->items[$user->id] = $user;

Breakdown:

So the method means:
“Place this user into the repository, using its ID as the key”.

If a user with that ID already exists, it is replaced.


5. public function all(): UserCollection { ... }

public function all(): UserCollection { ... }

This method returns all users as a UserCollection.

public
Anyone can call it.

all()
A simple name meaning “give me everything”.

: UserCollection
The method returns a UserCollection object.

Inside the method:

$collection = new UserCollection();

Create an empty collection.

foreach ($this->items as $user) {
    $collection->add($user);
}

For each stored user:

This transforms the internal array into a typed collection.

return $collection;

Return the finished collection.

So the whole method means:

“Build a UserCollection containing all stored users and return it”.


6. The Mental Model

Here's the class in plain English:

This is not production code.
It's a teaching tool that shows the shape of a repository without involving databases.


7. Why This Example Matters

This class introduces many new ideas:

Developers new to PHP need each one unpacked gently as a seed for further research.
This page gives us a quiet place to explore the syntax without interrupting the main Repositories page.

When you're ready, return to the main page
— the ideas will make more sense each time you revisit them.


Tony de Araujo —New York