There is a nice article about "When to declare classes final". A few quotes from it:

TL;DR: Make your classes always final, if they implement an interface, and no other public methods are defined

Why do I have to use final?

  1. Preventing massive inheritance chain of doom
  2. Encouraging composition
  3. Force the developer to think about user public API
  4. Force the developer to shrink an object's public API
  5. A final class can always be made extensible
  6. extends breaks encapsulation
  7. You don't need that flexibility
  8. You are free to change the code

When to avoid final:

Final classes only work effectively under following assumptions:

  1. There is an abstraction (interface) that the final class implements
  2. All of the public API of the final class is part of that interface

If one of these two pre-conditions is missing, then you will likely reach a point in time when you will make the class extensible, as your code is not truly relying on abstractions.

P.S. Thanks to @ocramius for great reading!

Answer from Nikita U. on Stack Overflow
🌐
PHP
php.net › manual › en › language.oop5.final.php
PHP: Final Keyword - Manual
The reason for this is you cannot unit test a class constant used in another class in isolation because you cannot mock a constant. Final methods allow you to have the same functionality as a constant while keeping your code loosely coupled. Tight coupling example (bad to use constants): <?php interface FooInterface { } class Foo implements FooInterface { const BAR = 1; public function __construct() { } } interface BazInterface { public function getFooBar(); } // This class cannot be unit tested in isolation because the actual class Foo must also be loaded to get the value of Foo::BAR class Ba
Top answer
1 of 5
74

There is a nice article about "When to declare classes final". A few quotes from it:

TL;DR: Make your classes always final, if they implement an interface, and no other public methods are defined

Why do I have to use final?

  1. Preventing massive inheritance chain of doom
  2. Encouraging composition
  3. Force the developer to think about user public API
  4. Force the developer to shrink an object's public API
  5. A final class can always be made extensible
  6. extends breaks encapsulation
  7. You don't need that flexibility
  8. You are free to change the code

When to avoid final:

Final classes only work effectively under following assumptions:

  1. There is an abstraction (interface) that the final class implements
  2. All of the public API of the final class is part of that interface

If one of these two pre-conditions is missing, then you will likely reach a point in time when you will make the class extensible, as your code is not truly relying on abstractions.

P.S. Thanks to @ocramius for great reading!

2 of 5
28

For general usage, I would recommend against making a class final. There might be some use cases where it makes sense: if you design a complex API / framework and want to make sure that users of your framework can override only the parts of the functionality that you want them to control it might make sense for you to restrict this possibility and make certain base classes final.

e.g. if you have an Integer class, it might make sense to make that final in order to keep users of your framework form overriding, say, the add(...) method in your class.

🌐
GeeksforGeeks
geeksforgeeks.org › php › what-are-the-final-class-and-final-method-in-php
What are the final class and final method in PHP ? - GeeksforGeeks
July 23, 2025 - There are some cases exists where we need to prevent the class from being inherited or from being overridden. For this, the final keyword can be utilized to accomplished the task in PHP that prevents any class, or method to inherit and override the functionality.
🌐
W3Schools
w3schools.com › php › keyword_final.asp
PHP final Keyword
The final keyword is used to prevent a class from being inherited and to prevent inherited method from being overridden. Read more about inheritance in our PHP OOP - Inheritance Tutorial.
🌐
Matthias Noback
matthiasnoback.nl › 2018 › 09 › final-classes-by-default-why
Final classes by default, why? | Matthias Noback
September 11, 2018 - After explaining good reasons for ... class and declare it “final”. PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final....
🌐
Medium
medium.com › @rcsofttech85 › final-classes-and-the-power-of-composition-in-php-205095193f05
Final Classes and the Power of Composition in PHP | by rahul chavan | Medium
March 22, 2024 - In this example, the Logger class is declared as final, indicating that it cannot be extended. This is particularly useful for classes representing utilities, services, or components where extending the behavior is unnecessary or even detrimental. Composition is an alternative to inheritance where objects are composed of other objects, allowing for greater flexibility and reusability. In PHP, composition can be achieved by creating classes that contain instances of other classes as members.
🌐
DEV Community
dev.to › erlandmuchasaj › final-and-readonly-classes-in-php-38fi
Final and Readonly Classes in PHP - DEV Community
March 21, 2023 - <?php namespace App\Utils; class ChildClass extends ParentClass { } So we have a ParentClass and a ChildClass that extends the parent. ... And this would output ChildClass public properties and also inherited properties from ParentClass as follows: Now if we do not want our class to be extended we just put the word final at the beginning of the class as follows:
🌐
TutorialsPoint
tutorialspoint.com › explain-final-class-and-final-method-in-php
Explain final class and final method in PHP.
In some cases, we might need to keep a class from being inherited from or we may need to prevent a function to be overridden. This can be achieved with the final, by prefixing the class and function with the final keyword.which essentially causes PHP to produced an error in the event that anybody attempts to extend a final class or override final function.
Find elsewhere
🌐
EDUCBA
educba.com › home › software development › software development tutorials › php tutorial › php final class
PHP Final Class | How PHP Final Class Works with Advantages
March 28, 2023 - PHP final class is a class which prevents overriding a method of the child classes just by the final prefix with the definition. It means that if we are defining a method with the final prefix then it is going to prevent overriding the method.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Top answer
1 of 7
72

I have often read that classes should only be made 'final' on rare/special occasions.

Whoever wrote that is wrong. Use final liberally, there’s nothing wrong with that. It documents that a class wasn’t designed with inheritance in mind, and this is usually true for all classes by default: designing a class that can be meaningfully inherited from takes more than just removing a final specifier; it takes a lot of care.

So using final by default is by no means bad. In fact, amongst OOP experts it’s widely agreed that final should be the default, e.g. Jon Skeet:

Classes should be sealed by default in C#

Or Joshua Bloch:

Design and document for inheritance or else prohibit it [Effective Java, 3rd Ed, Item 19]

Or Scott Meyers [More Effective C++, Item 33].

Which is why modern OO langauges such as Kotlin have final-by-default classes.

You wrote:

Maybe it screws up Mock object creation …

And this is indeed a caveat, but you can always recourse to interfaces if you need to mock your classes. This is certainly superior to making all classes open to inheritance just for the purpose of mocking.

2 of 7
8

If you want to leave a note to yourself that a class has no sub-classes, then by all means do so and use a comment, thats what they are for. The "final" keyword is not a comment, and using language keywords just to signal something to you (and only you would ever know what it means) is a bad idea.

edited by original author to add: I completely disagree with this now. I cannot even create a model of my mental state 11 years ago that would explain why I would say this. I think this answer, and my comments defending it below, are ridiculous. The accepted answer is right.

🌐
PHP.Watch
php.watch › versions › 8.1 › final-class-const
`final` class constants - PHP 8.1 • PHP.Watch
Attempting to use the final flag on class constants resulted in a fatal error. In PHP 8.1 and later, the final flag is allowed on class/interface constants, and none of the sub classes are allowed to extend/override final constants.
🌐
GitHub
ocramius.github.io › blog › when-to-declare-classes-final
When to declare classes final
TL;DR: Make your classes always final, if they implement an interface, and no other public methods are defined · In the last month, I had a few discussions about the usage of the final marker on PHP classes.
🌐
Tutorialspoint
tutorialspoint.com › php › php_final_keyword.htm
PHP - The Final Keyword
In PHP, the final keyword prevents classes and functions from being changed or overridden. It helps to keep important parts of your code secure, to guarantee no one accidentally breaks them while making changes.
🌐
Coursesweb
coursesweb.net › php-mysql › php-oop-final-classes-methods
PHP OOP - Final Classes and Methods
You can create subclasses to extend ... cannot be extended. Such a class is called final class. A final class is declared by adding the final keyword before the class word....
🌐
Szymon Krajewski
szymonkrajewski.pl › how-to-use-final-in-php
How to use final in PHP? — Szymon Krajewski
March 18, 2019 - Both ways lead to a proper solution, but because I’m the author of the class, I would like to encourage you to choose the safer option – composition. It prevents creating a chain of inheritance and preserves the encapsulation of behavior and state in class. Since there is no special reason why someone would need to extend ArticleProvider, I decided to mark them as final.
🌐
Symfony
cs.symfony.com › doc › rules › class_notation › final_class.html
Rule final_class - PHP Coding Standards Fixer
If you need to extend a standalone class, create an interface and use the Composite pattern. If these rules are too strict for you, you can use FinalInternalClassFixer instead. Risky when subclassing non-abstract classes. --- Original +++ New <?php -class MyApp {} +final class MyApp {}
🌐
DEV Community
dev.to › ialaminpro › understanding-the-final-keyword-in-php-preventing-inheritance-and-overriding-178j
Understanding the final Keyword in PHP: Preventing Inheritance and Overriding - DEV Community
September 10, 2024 - When working with Object-Oriented ... in your code. PHP provides the final keyword to prevent a class from being inherited or a method from being overridden in a subclass....
🌐
Reddit
reddit.com › r/php › design decisions: why use final classes
r/PHP on Reddit: Design Decisions: Why use final classes
December 1, 2022 - And it's not unknown for someone to raise an issue with your package and for their report to not mention the fact they extended your class, so you spend ages trying to resolve the issue and ultimately find out it was with their class which extends yours. Far simpler to declare a class final and prevent that from happening.