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

Enums Implementing Interfaces

PHP PHP 8.1+ Intermediate
debt(d3/e3/b3/t5)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches the common case' (d3). The detection_hints list phpstan and psalm as tools. Missing interface methods or incorrect return types are caught by these static analysers, which are near-default in modern PHP projects. The fatal error from trying to extend a class is caught at runtime instantly, but the more subtle mistakes (missing methods, wrong return types) need static analysis.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix describes adding `implements InterfaceName` to the enum declaration and implementing all required methods using `match($this)`. This is a small, localised refactor within one enum file — not a single-line patch but also not spanning multiple files.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The choice applies to a specific enum declaration. While it affects all consumers of that enum through the interface contract, the structural commitment is bounded to the enum and its direct usages. It doesn't impose a cross-cutting burden on the broader codebase.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field directly states the trap: developers expect enums to extend classes (as they might in other languages or with PHP classes), but enums can only implement interfaces because they already extend UnitEnum or BackedEnum internally. This is a documented gotcha that catches developers familiar with class-based OOP but new to PHP 8.1 enums.

About DEBT scoring →

TL;DR

PHP 8.1 enums can implement interfaces, allowing them to be used wherever an interface is expected — enabling polymorphic enum-based dispatch.

Explanation

Pure and backed enums can both implement interfaces. This enables: type-hinted parameters that accept enums (function process(Colorable $c)), enum cases used in collections alongside other objects implementing the same interface, and enum methods satisfying interface contracts. Enums cannot extend classes (they already extend UnitEnum/BackedEnum internally), but implementing multiple interfaces is allowed. Interface constants can be declared in enums too (PHP 8.3+). Common pattern: Status enum implementing HasLabel interface with getLabel() method.

Common Misconception

Enums can extend classes — they can only implement interfaces. Enums already extend UnitEnum or BackedEnum internally.

Why It Matters

Interface-implementing enums enable clean polymorphism without class hierarchies — enum cases become first-class participants in interface contracts.

Common Mistakes

  • Trying to extend a class with an enum — fatal error.
  • Not declaring required interface methods in the enum body.
  • Missing return types for interface methods in the enum implementation.

Code Examples

✗ Vulnerable
interface HasLabel {
    public function getLabel(): string;
}

// Without interface — no type safety:
enum Status { case Active; case Inactive; }
function render($status) { /* no type hint */ }
✓ Fixed
interface HasLabel {
    public function getLabel(): string;
}

enum Status: string implements HasLabel {
    case Active = 'active';
    case Inactive = 'inactive';

    public function getLabel(): string {
        return match($this) {
            Status::Active   => 'Active',
            Status::Inactive => 'Inactive',
        };
    }
}

function renderBadge(HasLabel $item): string {
    return '<span>' . $item->getLabel() . '</span>';
}

Added 22 Mar 2026
Edited 13 Jun 2026
Views 44
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 2 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Amazonbot 10 Scrapy 4 ChatGPT 3 Unknown AI 3 SEMrush 3 Ahrefs 3 Meta AI 2 Google 2 Perplexity 2 Claude 2 PetalBot 2 Bing 1
crawler 33 crawler_json 3 pre-tracking 1
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Add implements InterfaceName to enum declaration and implement all required methods using match($this) to dispatch per case.
📦 Applies To
PHP 8.1+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
enum .* {
Auto-detectable: ✗ No phpstan psalm
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Class Tests: Update

✓ schema.org compliant