{
    "slug": "php83_override_attr",
    "term": "#[Override] Attribute (PHP 8.3)",
    "category": "php",
    "difficulty": "intermediate",
    "short": "The #[Override] attribute tells PHP (and static analysers) that a method is intentionally overriding a parent class or interface method — if no such method exists in a parent, PHP throws an error at class load time.",
    "long": "In PHP before 8.3, you could write a method with the same name as a parent method and it would silently override it — or silently not override it if you misspelled the name. The #[Override] attribute makes the override intent explicit. When PHP loads a class with #[Override] on a method, it verifies that the method actually exists in a parent class or interface. If it doesn't — due to a rename, typo, or removed method — PHP throws an error immediately. This catches a class of silent bugs where a 'override' method is never called because it doesn't match the parent's signature.",
    "aliases": [
        "Override attribute",
        "PHP 8.3 Override",
        "@Override PHP"
    ],
    "tags": [
        "php8.3",
        "attributes",
        "oop",
        "inheritance",
        "refactoring"
    ],
    "misconception": "#[Override] changes how the method works or adds any runtime behaviour. It does not — it is purely a declaration that PHP validates at class load time. It has no effect on method dispatch, calling conventions, or performance.",
    "why_it_matters": "Renamed parent methods are a common source of subtle bugs — the child class method silently becomes a new method that is never called, and the parent's original implementation runs instead. #[Override] turns this silent failure into a load-time error. It is especially valuable during refactoring when parent APIs change: all child classes with #[Override] immediately fail if the parent method is renamed.",
    "common_mistakes": [
        "Not adding #[Override] to all override methods — it only helps if you use it consistently; adding it only sometimes gives false confidence.",
        "Confusing #[Override] with final — final prevents further overriding; #[Override] asserts that this method overrides something.",
        "Using #[Override] on methods that implement interface methods — this is valid and encouraged; interface method implementations count as overrides.",
        "Expecting #[Override] to validate signature compatibility — it only checks name existence, not parameter types or return types. Use PHPStan or Psalm for full signature checking."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php8",
        "attributes",
        "php8_attributes_intro",
        "abstract_classes",
        "interfaces",
        "liskov_substitution"
    ],
    "prerequisites": [],
    "refs": [
        "https://www.php.net/releases/8.3/en.php",
        "https://wiki.php.net/rfc/override_attribute"
    ],
    "bad_code": "<?php\n// ❌ Typo in override — silently creates a new method, parent's validate() runs\nclass UserValidator extends BaseValidator\n{\n    public function validae(mixed $data): bool // typo: validae not validate\n    {\n        return is_array($data) && isset($data['email']);\n    }\n    // BaseValidator::validate() still runs — this method is never called\n}",
    "good_code": "<?php\n// ✅ PHP 8.3 — #[Override] catches the typo at class load time\nclass UserValidator extends BaseValidator\n{\n    #[Override]\n    public function validate(mixed $data): bool // PHP errors if validate() not in parent\n    {\n        return is_array($data) && isset($data['email']);\n    }\n}\n\n// Also works with interface methods\nclass JsonRenderer implements RendererInterface\n{\n    #[Override]\n    public function render(array $data): string\n    {\n        return json_encode($data, JSON_THROW_ON_ERROR);\n    }\n}",
    "quick_fix": "Add #[Override] above any method that is intended to override a parent class or interface method. Run php -l or your test suite to immediately surface any mismatches.",
    "effort": "low",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php83_override_attr",
        "html_url": "https://codeclaritylab.com/glossary/php83_override_attr",
        "json_url": "https://codeclaritylab.com/glossary/php83_override_attr.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": "[#[Override] Attribute (PHP 8.3)](https://codeclaritylab.com/glossary/php83_override_attr) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php83_override_attr"
            }
        }
    }
}