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.

Discussions

Design Decisions: Why use final classes
I’m in the “final is probably wrong” camp as well. Has this guy ever heard of the private key word. Similarly, methods can be marked final no? Don’t just blanket prevent extension. That creates totally unnecessary headaches for consumers. For everyone recommending wrapping/decorators, what happens when the class you are decorating is an argument somewhere else? You change all the interfaces that interact with it too? And don’t give me this “just implement the interface” BS. Sometimes there isn’t one, and all the time I don’t want to have to recreate a bunch of functionality that already exists. Some classes have a big surface area! And to others who argue some version of “shooting yourself in the foot”. I’ll be the one to determine if extending and overriding is a good idea thank you very much! More on reddit.com
🌐 r/PHP
41
20
December 1, 2022
php - Why is using 'final' on a class really so bad? - Software Engineering Stack Exchange
I am refactoring a PHP OOP legacy website. I am so tempted to start using 'final' on classes to "make it explicit that the class is currently not extended by anything". This might save lots of t... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
July 2, 2011
Final classes by default, why?
I use a custom assertion provided by localheinz/test-util in auto-review tests to assert that production and test code classes are abstract or final. See, for example, SrcCodeTest and TestCodeTest . A rule for phpstan/phpstan would probably be better, but for now this helps to encourage composition over inheritance. More on reddit.com
🌐 r/PHP
30
37
June 6, 2018
Stop using final classes
There's no need to go replacing the class. If you can't use sanitizeFor(), you just decorate the service to override sanitize(). More on reddit.com
🌐 r/PHP
73
0
October 12, 2023
🌐
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.
🌐
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.
🌐
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....
🌐
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:
🌐
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.
Find elsewhere
🌐
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.
🌐
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.
🌐
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.
🌐
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....
🌐
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 {}
🌐
Medium
medium.com › @bandarans2000 › final-vs-readonly-in-php-laravel-whats-the-difference-d2360b880d96
Final vs Readonly in PHP & Laravel — What’s the Difference? | by Nadun Sameera | Medium
February 12, 2025 - When writing code in PHP or Laravel, you might come across the keywords final and readonly. They both help protect your code, but they do different things. Let’s break them down in a simple way with real-life examples 🚀. A final class means you can’t extend (inherit) it.
🌐
PHP
wiki.php.net › rfc › abstract_final_class
PHP: rfc:abstract_final_class
November 26, 2014 - When untestable/error prone code is mentioned is that there's no signaling of broken definition by creating a regular class with private constructor and other members are not declared as static. Everything will just compile smoothly (file inclusion/autoloading). By the acceptance of this patch, every method declaration would automatically be enforced to behave as static without any further keyword needed. This reduces the human error of methods being forgotten to be declared as static. ... Could also be used with combination of “final”, meaning it cannot be extended (such as more permissive visibility, behavior change, etc)
🌐
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.

🌐
Verraes
verraes.net › 2014 › 05 › final-classes-in-php
Final Classes: Open for Extension, Closed for Inheritance
May 12, 2014 - By making a class inheritable, you are saying to the outside world: this class is meant to be extended. In some cases that may be the best option, but it is not the only option. Declaring explicitly what the extension points are, is part of the contract your code has with the rest of the system. Final classes help to enforce this contract.