User Tools

Site Tools


essentials:match_declarative_branching

5. 'Match' — Declarative Branching With Shape and Intent

Modern PHP gives us a more expressive way to handle branching logic.
match is not a prettier switch.
It’s a declarative expression that returns a value, enforces completeness, and makes intent explicit.

It reflects how PHP 8+ wants us to think:

  • branching should be predictable, exhaustive, and shaped like data.

AI gets match wrong often.
It mixes it with switch, forgets exhaustiveness,
or uses it where a simple if would be clearer.


1. What 'match' Is

match is an expression that:

  • compares a value against patterns
  • returns a result
  • requires all cases to be handled
  • never falls through
  • never silently ignores missing branches

It’s branching with guardrails.


2. Why Modern PHP Uses 'match'

match gives us:

  • clearer intent
  • predictable outcomes
  • no fall‑through bugs
  • no accidental missing cases
  • a return value that fits neatly into expressions

match is ideal when branching is structural,
not behavioral.


3. The Mental Model

match is data‑shaped branching.

It’s not about control flow.
match is about mapping one-value-to-another
in a way that is:

  • explicit
  • complete
  • predictable

The key idea:

  • To use match when we’re selecting a value,
    not executing steps, calling services, or performing side effects (like state changes).

4. A Simple Example

$status = match ($code) {
    200 => 'OK',
    404 => 'Not Found',
    500 => 'Server Error',
};

This is not a control structure.
It’s a value expression.


5. When to Use 'match'

Use match when:

  • we're mapping one value to another
  • all cases should be handled
  • we want a predictable return value
  • we want to avoid fall‑through
  • the branching is declarative, not procedural

It shines in DTOs, controllers, transformers, and small decision boundaries.


6. When Not to Use match

Avoid match when:

  • we need side effects like changes to the application's state or behavior
  • we're performing work, not selecting a value
  • the branching depends on complex conditions
  • readability suffers
  • the logic belongs in a method or object instead

match is for selection,
not execution
.

When AI Gets This Wrong

AI often treats match like a modern switch, using it for side effects or procedural logic.
Sometimes it forgets to handle all cases, mixes types, or nests match expressions unnecessarily.
Our mental model helps us see when match is the right tool
— and when AI is using it simply because it “looks modern”.


Summary

match gives us a declarative, predictable way to map values.
It enforces completeness, avoids fall‑through, and keeps branching aligned with the shape of the data.

Once we internalize this,
we can immediately see when AI‑generated code:

  1. uses match for work instead of selection
  2. forgets to handle all cases
  3. mixes types in a way that breaks exhaustiveness
  4. or misses opportunities to use match where it clarifies intent

match is a small feature
— but it teaches us how modern PHP wants to express decisions.

A tiny aside:
To match or not to match…
every match expression begins with a question.
The important part is choosing the right stage for the answer.


essentials/match_declarative_branching.txt · Last modified: by editor