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

#[Override] Attribute (PHP 8.3)

PHP PHP 8.3+ Intermediate
debt(d1/e1/b3/t3)
d1 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'caught instantly' (d1). The #[Override] attribute is validated at class load time by the PHP engine itself — a mismatch between the attribute and an actual parent/interface method produces a fatal error immediately, before any code runs. No external tool is needed; the runtime is the detector.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix states: add #[Override] above the method, or remove/rename to match the parent. Either direction is a single-line change with no refactoring required.

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

Closest to 'localised tax' (b3). The attribute applies to individual OOP method declarations in web/cli contexts. Teams that adopt it consistently pay a small ongoing cost of annotating override methods, but the rest of the codebase is unaffected. The common_mistake about partial adoption (false confidence) raises it slightly above b1, but it does not spread cross-cutting complexity.

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

Closest to 'minor surprise' (t3). The misconception field identifies the main trap: developers may believe #[Override] changes runtime behaviour or method dispatch, when it is purely a compile-time declaration with no runtime effect. This is a single, well-bounded edge case rather than a deep behavioural contradiction, so t3 fits — slightly above t1 because the 'it does something at runtime' assumption is common for similar annotations in Java/C#.

About DEBT scoring →

Also Known As

Override attribute PHP 8.3 Override @Override PHP

TL;DR

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.

Explanation

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.

Watch Out

#[Override] only checks that a method with the same name exists in a parent — it does NOT verify that the signature (parameters, return type, visibility) matches, so you can still override incorrectly and PHP will only warn if a static analyser is configured to catch signature mismatches.

Common 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.

Avoid When

  • When a method is new to your class and does not override any parent method or interface contract — #[Override] will cause a load-time error.
  • When working with dynamic or magic method patterns (__call, __get) where the 'override' relationship is intentionally implicit and not tied to a named parent method.
  • When supporting legacy code where adding #[Override] to existing methods would require immediate refactoring of parent classes that cannot yet be updated.

When To Use

  • You're refactoring a parent class and renaming or removing methods — mark all child implementations with #[Override] to catch accidental breaks in inheritance chains.
  • You're working in a team where method names can be typo'd or misremembered — #[Override] turns silent bugs into immediate load-time errors that static analysers and CI pipelines will catch.
  • You're maintaining a large codebase with deep inheritance hierarchies and need to document intent clearly — #[Override] makes it explicit that a method is intentional, not a leftover copy-paste.
  • You're using a strict code standard or IDE that supports PHP 8.3+ attributes — applying #[Override] systematically improves type safety and refactoring confidence.

Code Examples

💡 Note
A good example shows #[Override] catching a typo in a method name that would have silently failed to override; a bad example shows misusing #[Override] on a method that doesn't override anything, triggering an immediate error.
✗ Vulnerable
<?php
// ❌ Typo in override — silently creates a new method, parent's validate() runs
class UserValidator extends BaseValidator
{
    public function validae(mixed $data): bool // typo: validae not validate
    {
        return is_array($data) && isset($data['email']);
    }
    // BaseValidator::validate() still runs — this method is never called
}
✓ Fixed
<?php
// ✅ PHP 8.3 — #[Override] catches the typo at class load time
class UserValidator extends BaseValidator
{
    #[Override]
    public function validate(mixed $data): bool // PHP errors if validate() not in parent
    {
        return is_array($data) && isset($data['email']);
    }
}

// Also works with interface methods
class JsonRenderer implements RendererInterface
{
    #[Override]
    public function render(array $data): string
    {
        return json_encode($data, JSON_THROW_ON_ERROR);
    }
}

Added 23 Mar 2026
Edited 19 Jun 2026
Views 92
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 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 2 pings W 1 ping T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Amazonbot 16 Perplexity 11 Google 4 ChatGPT 3 Ahrefs 3 Scrapy 3 Majestic 2 Claude 2 PetalBot 2 Meta AI 1 Bing 1
crawler 45 crawler_json 3
DEV INTEL Tools & Severity
⚙ Fix effort: Low
⚡ 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.
📦 Applies To
PHP 8.3+ web cli


✓ schema.org compliant