Table of Contents
10. Entities — When Identity Matters
Some concepts in our system are more than values.
They are not just cards or small pieces of meaning.
They are… “things” that continue to exist over time.
A user.
A ticket.
An order.
A blog post.
These are not “just data”.
They have a life story.
That's what makes them entities.
1. Before Any Code: What Is an Entity?
Let's return to the desk metaphor.
Imagine a folder labeled “User #17”.
Inside the folder, we keep:
- a name
- an age
- whether the user is active
- maybe a list of posts
- maybe a history of actions
Now imagine that the person changes their name.
Or updates their email.
O
r becomes inactive.
We don't throw away the folder.
We update what's inside.
The folder — the identity — stays the same.
That's the essence of an entity:
- An entity is a concept
whose identity stays the same
even as its data changes.
Unlike “Value Objects”,
entities are defined by a unique identifier (like a primary key)
rather than just their attributes.
2. Identity vs. Meaning
This is where entities differ from value objects.
- A value object is a small piece of meaning.
If the meaning changes, it becomes a different value. - An entity is a long‑lived concept.
If its data changes, it is still the same entity.
A person who changes their name is still the same person.
A price that changes is not the same price.
Identity is the anchor.
3. Giving the Entity a Shape
Here's a simple entity class:
class User { public int $id; public string $name; public bool $isActive; }
Let's slow down:
- class User — we're defining a concept
$id— the unique identifier$name— the user's name$isActive— whether the user is active
The id is what makes this user this user.
Everything else can evolve.
This is the shape of a long‑lived concept.
4. Creating an Entity
$user1 = new User(); $user1->id = 17; $user1->name = "Tony"; $user1->isActive = true;
Using our metaphor:
new User()creates a new “User” folder$user1is the label we put on that folder$user1→idis the unique identifier inside the folder$user1→nameand$user1→isActiveare the details that can change
The identity is the folder itself.
The contents can be updated.
5. Entities Change — Identity Does Not
Let's update the user:
$user1->name = "Anthony"; $user1->isActive = false;
The folder is still “User #17”.
Only the contents changed.
This is the core idea:
Value objects never change. Entities change all the time — but remain themselves.
6. Adding Behavior — Rules That Belong to the Entity
Entities often carry rules tied to their identity.
class User { public int $id; public string $name; public bool $isActive; public function deactivate(): void { $this->isActive = false; } }
Let's unpack:
deactivate()is something the user can do$thismeans “this user”$this→isActive = falseupdates the folder
This rule belongs with the user.
It's part of the concept.
7. Why Entities Matter
Entities give us:
- identity — a stable reference
- continuity — the concept persists
- behavior — rules tied to the lifecycle
- structure — a clear shape
- safety — fewer invalid states
Entities are the long‑lived concepts in our system.
8. When Entities Are the Right Tool
Use an entity when the concept:
- has identity
- persists over time
- can change
- appears in multiple places
- has rules tied to its lifecycle
- is central to our system
Examples:
- User
- Order
- Invoice
- BlogPost
- Ticket
- Product
These are not “just values”.
They are the living parts of our domain.
9. When Entities Are Too Much
Avoid entities when the data is:
- small
- immutable
- just a value
- not meaningful
- not long‑lived
Those belong to value objects.
10. The Mental Model
Entities are long‑lived concepts with identity.
They evolve.
They carry rules.
They tell the story of our system over time.
Use them when:
- the concept continues
- the attributes change
- the identity matters
Entities are the characters in our system's story.
11. When to Ask AI for an Entity
The moment when identity matters
Conversation Example
We ask:
“Create a Post with title and body.”
AI gives us a simple class.
Then we ask:
“Add comments to the post.”
AI gives us:
$post->comments[] = $comment;
Then we ask:
“Allow editing the post later.”
AI adds setters.
Then we ask:
“Store posts in a database.”
AI adds an ID.
Now we feel it:
- this object changes over time
- it has identity
- it has lifecycle
- it has relationships
This is the moment to say:
“This is an Entity. Give it an ID and treat it as long‑lived.”
Rule of Thumb
If something changes over time
or has identity — ask for an Entity.
Summary
This page is part of a gentle introduction to modern PHP.
Objects are not about complexity — they are about clarity.
When our data starts to feel like a “thing”, give it a name and let it carry the
small actions that belong to it.
Tony de Araujo —New York
