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

Enums Implementing Interfaces

php PHP 8.1+ Intermediate

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
Views 24
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 8 Unknown AI 3 Google 2 Perplexity 2 SEMrush 2 ChatGPT 1 Meta AI 1 Ahrefs 1
crawler 19 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