When you extend a PHP class, it is easy to misspell a method name and create a new method instead of replacing the parent method. PHP's built-in #[\Override] attribute makes that intention explicit. The engine checks that a matching method exists in a parent class or an implemented interface. If it does not, PHP reports an error instead of silently accepting the declaration.
The attribute became available for methods in PHP 8.3. It is useful in class hierarchies, especially when you rename methods or update interfaces over time. It does not replace normal method-signature rules; PHP still enforces compatible declarations.
Mark a method override
Place #[\Override] immediately before the child method. The leading backslash refers to PHP's built-in attribute in the global namespace, even when your code lives in a named namespace. The child method must match an inherited or interface method name.
Example: Override a parent's formatter method
<?php
// The attribute checks that the child method matches a parent method.
class LabelFormatter
{
public function label(string $name): string
{
return $name;
}
}
class GreetingFormatter extends LabelFormatter
{
#[\Override]
public function label(string $name): string
{
return "Hello, $name!";
}
}
echo (new GreetingFormatter())->label('Asha'), PHP_EOL;
The output is:
Hello, Asha!
The child method changes the label while keeping the same method contract. Remove the attribute and the program still runs, but you lose the check that the method is truly overriding something.
Catch a spelling mistake early
Consider a parent method named label(). If the child accidentally declares lable(), PHP normally treats it as a separate method. With #[\Override], that mistake becomes an error during class loading. The following snippet is intentionally invalid, so do not add it to a runnable demo that should succeed.
Example: An invalid override
<?php
class ParentFormatter
{
public function label(): string { return 'Parent'; }
}
class ChildFormatter extends ParentFormatter
{
// Typo: "lable" does not match the parent's "label".
#[\Override]
public function lable(): string { return 'Child'; }
}
PHP reports that ChildFormatter::lable() has the Override attribute but no matching parent method exists. The exact error text can vary by PHP version. Correct the method name rather than removing the attribute to hide the mismatch.
Implement an interface method
The attribute also works when a class fulfills an interface contract. This helps document that the method is part of a declared API, not merely a similarly named helper.
Example: Implement an interface method
<?php
interface MessageWriter
{
public function write(string $message): void;
}
class ConsoleWriter implements MessageWriter
{
#[\Override]
public function write(string $message): void
{
// Fulfill the interface method.
echo $message, PHP_EOL;
}
}
(new ConsoleWriter())->write('Saved');
This prints Saved. Interface declarations already require an implementation; the attribute adds an explicit signal to readers and tools about the method's role.
Version limits and practical use
Use a PHP 8.3 or newer runtime for method examples. Starting with PHP 8.5, the attribute can also mark overriding properties. If you target PHP 8.3 or 8.4, keep it on methods only. PHP does not allow it on __construct(), because constructors are exempt from the usual signature checks.
- Add the attribute when a child method is meant to replace an inherited method or implement an interface method.
- Keep method names and signatures compatible with the parent or interface.
- Run your tests after renaming parent methods; the attribute will expose children that no longer match.
- Do not add it to an unrelated new method just to label it as important.
Tip: Use #[\Override] consistently in a class hierarchy. It makes intentional overrides easier to spot during review and turns accidental name drift into an immediate error.
Conclusion
PHP's #[\Override] attribute is a small safeguard for inheritance and interfaces. Put it above an overriding method, and PHP checks that a matching declaration exists. It helps you catch typos and refactoring mistakes before they become surprising behavior, while ordinary compatibility checks continue to apply.