#[Override] Attribute (PHP 8.3)
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);
}
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
68
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 14
Perplexity 11
ChatGPT 3
Google 2
Majestic 1
Ahrefs 1
Also referenced
How they use it
crawler 31
crawler_json 1
Related categories
⚡
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