The Final Word

Among the list used keywords in php is “final”

The definition from

PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

If you are anything like me, then your first thought must have been why would I want to kill inheritance and polymorphism. This does seem like a needless, neigh, a downright harmful operation. However this keyword plays a very important role in your application design.

One use case is where you would want to carry out the Introduce Parameter Object Refactor .

Our original class looks like this:

Once we have refactored:


So far all is well. Now imagine way into the future, you decide to make the date output a bit more user friendly, and being a good developer you extend the DateRange class with a new method

While all may look well. This bit of code will break all classes that used it as parameter!

When you really think about it, you realize that DateRange had no business being extended and should have been treated as a primitive object, just like the parameters it replaced.

As such this class should be declared final to protect it from further meddling and even more importantly to ensure consistency of your code.



Published by


Software Project Manager