Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Final in PHP?

Tags:

oop

php

final

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'.

like image 439
Inga Johansson Avatar asked Nov 22 '10 17:11

Inga Johansson


People also ask

What is use of final in PHP?

The final keyword is used to prevent a class from being inherited and to prevent inherited method from being overridden.

Why do we use final?

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".

What does final mean in a PHP class?

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.

Where can we use final?

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.


2 Answers

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!

like image 144
Nikita U. Avatar answered Sep 20 '22 15:09

Nikita U.


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.

like image 26
mhanisch Avatar answered Sep 17 '22 15:09

mhanisch