I know what the definition is of a Final class, but I want to know how and when final is really needed.
<?php final class Foo extends Bar { public function() { echo 'John Doe'; } }
If I understand it correctly, 'final' enables it to extend 'Foo'.
Can anyone explain when and why 'final' should be used? In other words, is there any reason why a class should not be extended?
If for example class 'Bar' and class 'Foo' are missing some functionality, it would be nice to create a class which extends 'Bar'.
The final keyword is used to prevent a class from being inherited and to prevent inherited method from being overridden.
The final keyword is a non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override). The final keyword is useful when you want a variable to always store the same value, like PI (3.14159...). The final keyword is called a "modifier".
Final Keyword ¶ The final keyword prevents child classes from overriding a method or constant by prefixing the definition with final . If the class itself is being defined final then it cannot be extended.
It can be used with variables, methods, and classes. Once any entity (variable, method or class) is declared final , it can be assigned only once. That is, the final variable cannot be reinitialized with another value.
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 definedWhy do I have to use
final
?
- Preventing massive inheritance chain of doom
- Encouraging composition
- Force the developer to think about user public API
- Force the developer to shrink an object's public API
- A
final
class can always be made extensibleextends
breaks encapsulation- You don't need that flexibility
- You are free to change the code
When to avoid
final
:Final classes only work effectively under following assumptions:
- There is an abstraction (interface) that the final class implements
- 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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With