Interview frequency: …
Short answer
It is placed on derived methods to confirm that they override a method that exists in the parent class or interface; the engine throws a compile-time error if the original method changes.
Explanation
In a large project, you may override a parent method, then another developer may rename or delete that method. Your derived method silently becomes a new method that is never called, creating hidden bugs. Adding #[\Override] makes the PHP engine verify that the parent method exists and throw a Compile Error immediately if it does not.
Code example
namespace App\Models;
class ParentModel
{
public function execute(): void {}
}
class ChildModel extends ParentModel
{
#[\Override] // the leading backslash matters inside a namespace
public function execute(): void {}
// #[\Override]
// public function exceute(): void {} // typo → Fatal error: ChildModel::exceute() has #[\Override]
// // attribute, but no matching parent method exists
}Common mistakes
- Placing #[\Override] on a completely new method that implements no interface method and overrides no parent method.