User Tools

Site Tools


annex1:explanation_for_inmemory_userrepository

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:

  • “You must have a find() method”.
  • “You must have a save() method”.
  • “You must have an all() method”.

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:

  • a User object
  • or null

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 → “this repository”
  • →items → “open the internal array”
  • [$id] → “look for the user with this ID”
  • ?? null → “if it doesn’t exist, return null”

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:

  • $this→items → open the internal array
  • [$user→id] → use the user's ID as the key
  • = $user → store the user there

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:

  • open the collection
  • add the 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:

  • The repository is a filing cabinet.
  • $items is the drawer inside the cabinet.
  • save() puts a folder (User) into the drawer.
  • find() retrieves a folder by its ID.
  • all() returns a box (UserCollection) containing all folders.
  • Everything happens in memory — nothing is written to disk.

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:

  • interfaces
  • visibility (private, public)
  • typed properties
  • typed parameters
  • nullable return types (?User)
  • $this
  • array access ([$id])
  • null‑coalescing (??)
  • foreach loops
  • returning collections

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

annex1/explanation_for_inmemory_userrepository.txt · Last modified: by editor