{
    "slug": "php_enum_interface",
    "term": "Enums Implementing Interfaces",
    "category": "php",
    "difficulty": "intermediate",
    "short": "PHP 8.1 enums can implement interfaces, allowing them to be used wherever an interface is expected — enabling polymorphic enum-based dispatch.",
    "long": "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.",
    "aliases": [],
    "tags": [
        "php",
        "enums",
        "interfaces",
        "php81"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "enums",
        "interfaces",
        "php_enum_cases_method",
        "match_expression"
    ],
    "prerequisites": [
        "enums",
        "interfaces"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.enumerations.interfaces.php"
    ],
    "bad_code": "interface HasLabel {\n    public function getLabel(): string;\n}\n\n// Without interface — no type safety:\nenum Status { case Active; case Inactive; }\nfunction render($status) { /* no type hint */ }",
    "good_code": "interface HasLabel {\n    public function getLabel(): string;\n}\n\nenum Status: string implements HasLabel {\n    case Active = 'active';\n    case Inactive = 'inactive';\n\n    public function getLabel(): string {\n        return match($this) {\n            Status::Active   => 'Active',\n            Status::Inactive => 'Inactive',\n        };\n    }\n}\n\nfunction renderBadge(HasLabel $item): string {\n    return '<span>' . $item->getLabel() . '</span>';\n}",
    "quick_fix": "Add implements InterfaceName to enum declaration and implement all required methods using match($this) to dispatch per case.",
    "severity": "info",
    "effort": "low",
    "created": "2026-03-22",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_enum_interface",
        "html_url": "https://codeclaritylab.com/glossary/php_enum_interface",
        "json_url": "https://codeclaritylab.com/glossary/php_enum_interface.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Enums Implementing Interfaces](https://codeclaritylab.com/glossary/php_enum_interface) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_enum_interface"
            }
        }
    }
}