Get your next remote job on LaraJobs.
0 comments 1 contributor Edit on GitHub PHP

PHP 8.3's Override attribute in a nutshell

PHP 8.3's Override attribute in a nutshell

What does the Override attribute do?

When you are using PHP 8.3’s new Override attribute, here’s what you tell other developers or your future self:

  • I’m overriding a method from the parent class or one of the implemented interfaces.
  • I have correctly overridden the method. If, for instance, I made a typo, the code would throw an error.

I had so much trouble understanding this one. But in fact, it’s dead simple. 😅

Here’s an example:

class SomeClass
{
public function someMethod()
{
}
}
 
class SomeOtherClass extends SomeClass
{
public function someMethod()
{
}
}

In this example, SomeOtherClass overrides the someMethod method from its parent class, SomeClass. But what happens if we change someMethod in SomeClass? Our override will be completely nullified.

But as you guessed it, the solution to this problem is the Override attribute:

class SomeClass
{
// We renamed the method.
public function someRenamedMethod()
{
}
}
 
class SomeOtherClass extends SomeClass
{
// But now, thanks to Override, we get an error message saying
// that our override doesn't override any method anymore.
//
// Fatal error: SomeOtherClass::someMethod() has #[\Override] attribute, but no matching parent method exists
 
#[Override]
public function someMethod()
{
}
}

How neat is that? 👌

When to use Override

Let’s look at some scenarios where Override is incredibly useful:

  • Accidental method names: Imagine you’re working on a class that extends another. You add a new method, thinking you’re overriding a parent method. But, oops, you got the method name wrong. Without Override, PHP wouldn’t warn you, leading to potential issues. With Override, PHP immediately alerts you to the mistake.
  • Interface changes: You have a class implementing an interface. Later, the interface changes, maybe removing or renaming a method. Without Override, you might not realize your class no longer correctly implements the interface. With the attribute in place, PHP would flag this change, prompting you to update your class.

Why did the PHP team choose the Override attribute over a new keyword?

You might wonder why the people working on PHP used the Override attribute instead of a new keyword (like final and readonly for instance). The reason is twofold:

  • Backward compatibility: Using an attribute doesn’t require changes to the PHP parser, making it backward compatible. You can add it to existing codebases without issues.
  • Non-behavioral: The attribute doesn’t change how the method works or is called. It’s purely for validation during development, making it less intrusive.

Limitations of Override and its future

Currently, Override is limited to methods. Properties, another key part of classes, don’t yet have a similar feature. However, the future might bring more enhancements, like class-level attributes or directives to enforce the use of Override.

Be the first to comment!

Get help or share something of value with other readers!

Great deals for enterprise developers
The latest community links

Recommended articles

PHP 8.3 is out, now! Here's what's new and changed.

PHP 8.3 was released on November 23, 2023, and as usual, you need to be up to date with new features and breaking changes for easier transitions.

PHP
20+ Laravel best practices, tips and tricks to use in 2024

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

7 Laravel RESTful APIs best practices for 2024

Master the art of crafting RESTful APIs with Laravel thanks to these best practices.

Laravel interview questions and answers for 2024

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

9 testing best practices for Laravel in 2024

Are you familiar with testing? Good. Here are a bunch of best practices to help you level up even more!

How does Laravel work? A crystal clear explanation.

Discover my step by step and simple explanation of how Laravel makes your life easier.

Laravel 11: the big changes ahead and release date

Laravel 11 will be released on February 6th, 2024. Its development is still ongoing. Let's dive into every relevant new feature we know about already.

3 crucial Laravel architecture best practices for 2024

Explore Laravel's default architecture to simplify collaboration, compatibility, and onboarding.

- / -