Table of Contents
7. Enums — Giving Meaningful Shape to Known Values
Enums solve one of the oldest problems in PHP:
representing a closed set of meaningful values without magic strings or loose integers.
Enums are one of the most expressive additions to modern PHP.
They let us represent a closed set of values with clarity, type safety, and intention
— without relying on magic strings, integers, or scattered constants.
They reflect a broader shift in PHP 8.1+:
values should have
shape, meaning,
and a home.
1. What Enums Are
Enums define a fixed set of possible values.
Each value is a real object with identity, type, and meaning.
enum Status { case Draft; case Published; case Archived; }
No strings.
No integers.
No guessing.
Just clear, intentional values.
2. Why Modern PHP Uses Them
| Enums give us: | -type safety, -self‑documenting code, -predictable value sets, -fewer runtime surprises, -a single source of truth |
They replace:
- magic strings ('draft', 'published')
- integer codes (0, 1, 2),
- scattered constants,
- fragile switch statements
Enums make the domain more explicit
— and easier for both humans and AI to understand.
3. The Mental Model
Enums are named values with identity.
- They’re not just labels.
not just constants
not just strings with better marketing.
Each enum case is a real object
that represents a meaningful state in our domain.
The key idea:
Use enums when a value has meaning,
not just content.
4. A Simple Example
enum Role { case Admin; case Editor; case Viewer; } function canEdit(Role $role): bool { return match ($role) { Role::Admin, Role::Editor => true, Role::Viewer => false, }; }
The code becomes clearer because the values themselves carry intent.
5. Backed Enums
Sometimes we need to store or transmit the value.
Backed enums give each case a scalar representation:
enum Currency: string { case USD = 'usd'; case EUR = 'eur'; case GBP = 'gbp'; }
You get the best of both worlds:
- meaningful values in code
- stable scalars for storage or APIs
6. When to Use Enums
Use enums when:
- a value belongs to a closed set
- the value has meaning in the domain
- we want type safety
- we want to avoid magic strings
- we want clarity at the call site
- we want AI to stop inventing inconsistent values
Enums shine in DTOs, domain models, controllers, and API boundaries.
7. When Not to Use Enums
Avoid enums when:
- the set of values is open‑ended
- the values come from user input
- the values change frequently
- the meaning belongs in a domain object
- we need behavior, not just identity
Enums are for meaningful values, not dynamic data.
When AI Gets This Wrong
AI often invents enum cases that don’t exist, mixes backed and unbacked enums, or uses enums where a simple string is more appropriate.
Sometimes it treats enums as if they should contain behavior, or nests enums inside enums.
Our mental model helps us see when an enum expresses real meaning
—
and when AI is using one simply because it “looks structured”.
Summary
Enums give shape to meaningful values.
They replace magic strings, reduce ambiguity,
and make the domain more explicit.
Once we internalize this,
we can immediately see when AI‑generated code:
- invents enum cases
- mixes enum types
- uses enums for dynamic data
- or misses opportunities to clarify meaning
Enums are a small feature
— but they teach us how modern PHP wants to express identity and intention.
