← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

State Pattern

quality PHP 5.0+ Intermediate

Also Known As

state machine pattern finite state machine state design pattern

TL;DR

Encapsulates the varying behaviour of an object based on its internal state into separate state objects, eliminating state-based conditionals.

Explanation

The State pattern models an object whose behaviour changes based on its current state by delegating to a State object. Each concrete state implements the same interface, and the context simply forwards calls to the current state. Transitioning between states is handled by the state objects themselves or the context. This replaces sprawling if/switch chains on a status field with polymorphism, follows Open/Closed Principle (new states = new classes, not new branches), and localises state-specific behaviour. In PHP, common examples include order workflows, connection states, and document approval processes.

Common Misconception

A switch statement on a status field is equivalent to the state pattern. A switch scatters state-specific logic across the codebase — the state pattern encapsulates each state's behaviour in its own class, making new states addable without modifying existing code.

Why It Matters

The State pattern replaces complex conditionals checking internal state with polymorphism — each state is a class, transitions are explicit, and adding a new state does not require modifying existing states.

Common Mistakes

  • Implementing state as a string or integer flag with switch statements — the anti-pattern the State pattern replaces.
  • State classes with too much business logic — they should manage transitions and delegate business logic to the context.
  • Not preventing invalid transitions — every state should only allow transitions to valid next states.
  • Overusing State pattern for objects with 2-3 states where a simple boolean or enum suffices.

Code Examples

✗ Vulnerable
// Switch on status string — grows with every new state:
function handleOrder(Order $o): void {
    switch ($o->status) {
        case 'pending': /* ... */ break;
        case 'paid': /* ... */ break;
        case 'shipped': /* ... */ break;
        // Each new status requires modifying this switch
    }
}
✓ Fixed
interface OrderState {
    public function pay(Order $order): void;
    public function ship(Order $order): void;
    public function cancel(Order $order): void;
}

class PendingState implements OrderState {
    public function pay(Order $o): void    { $o->setState(new PaidState()); }
    public function ship(Order $o): void   { throw new \LogicException('Pay first'); }
    public function cancel(Order $o): void { $o->setState(new CancelledState()); }
}

class PaidState implements OrderState {
    public function pay(Order $o): void    { throw new \LogicException('Already paid'); }
    public function ship(Order $o): void   { $o->setState(new ShippedState()); }
    public function cancel(Order $o): void { $o->setState(new RefundingState()); }
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 18
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 2 pings T 0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 8 Perplexity 2 Unknown AI 2 ChatGPT 2 Ahrefs 1 Google 1
crawler 15 crawler_json 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: High
⚡ Quick Fix
Replace switch-on-status with a State object per state — each state implements the same interface but with state-specific behaviour, and transitions are handled by the states themselves
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
switch($order->status) or if($status === 'pending') branching in multiple methods duplicating state checks
Auto-detectable: ✗ No phpstan psalm
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Class Tests: Update

✓ schema.org compliant