# When to declare classes final

DevFeed: [When to declare classes final](<https://devfeed.tech/articles/when-to-declare-classes-final-21149.md>)

Original publisher: [Read original article](<https://ocramius.github.io/blog/when-to-declare-classes-final/>)

Published: 2015-01-06T00:00:00Z

Content type: opinion

Language: en

Sources: [Marco Pivetta](<https://devfeed.tech/sources/marco-pivetta.md>)

Topics: [PHP](<https://devfeed.tech/topics/php.md>), [Object-oriented programming (OOP)](<https://devfeed.tech/topics/oop.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Refactoring](<https://devfeed.tech/topics/refactoring.md>)

Tags: [articles](<https://devfeed.tech/tags/articles.md>), [doom](<https://devfeed.tech/tags/doom.md>), [examples](<https://devfeed.tech/tags/examples.md>), [inheritance](<https://devfeed.tech/tags/inheritance.md>), [oop](<https://devfeed.tech/tags/oop.md>), [opinion](<https://devfeed.tech/tags/opinion.md>), [php](<https://devfeed.tech/tags/php.md>), [programming](<https://devfeed.tech/tags/programming.md>)

## AI overview

This opinion article argues that PHP classes should generally be declared final, especially when they implement an interface and expose no other public methods. It explains that preventing inheritance can discourage deep inheritance chains, encourage composition, and make developers design clearer public APIs.

## Source excerpt

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. The pattern is recurrent: I ask for a newly introduced class to be declared as final the author of the code is reluctant to this proposal, stating that final limits flexibility I have to explain that flexibility comes from good abstractions, and not from inheritance It is therefore clear that coders need a better explanation of when to use final, and when to avoid it. There are many other articles about the subject, but this is mainly thought as a "quick reference" for those that will ask me the same questions in future. When to use "final": final should be used whenever possible. Why do I have to use final? There are numerous reasons to mark a class as final: I will list and describe those that are most relevant in my opinion. 1. Preventing massive inheritance chain of doom Developers have the bad habit of fixing problems by providing specific subclasses of an existing (not adequate) solution. You probably saw it yourself with examples like following: <?php class Db { /* ... */ } class Core extends Db { /* ... */ } class User extends Core { /* ... */ } class Admin extends User { /* ... */ } class Bot extends Admin { /* ... */ } class BotThatDoesSpecialThings extends Bot { /* ... */ } class PatchedBot extends BotThatDoesSpecialThings { /* ... */ } This is, without any doubts, how you should NOT design your code. The approach described above is usually adopted by developers who confuse OOP with "a way of solving problems via inheritance" ("inheritance-oriented-programming", maybe?). 2. Encouraging composition In general, preventing inheritance in a forceful way (by default) has the nice advantage of making developers think more about composition. There will be less stuffing functionality in existing code via inheritance, which, in my opinion, is a symptom of haste