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

#[Override] Attribute (PHP 8.3)

php PHP 8.3+ Intermediate

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.

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.

Code Examples

✗ 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
Views 68
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 3 pings M 0 pings T 0 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 0 pings F 0 pings S 0 pings S 2 pings M 0 pings T 0 pings W 2 pings T 2 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 14 Perplexity 11 ChatGPT 3 Google 2 Majestic 1 Ahrefs 1
crawler 31 crawler_json 1
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