You have probably worked with classes in PHP many times and know the basics of object orientation. But what happens if you want to ensure that a method in a base class cannot be overridden by a derived class? This is where the keyword final comes into play. This powerful tool gives you the ability to maintain the integrity of your class hierarchy and protect certain methods from changes. In this guide, you will learn how to use final effectively to prevent unwanted modifications of methods.
Key insights
The final keyword in PHP is used to prevent overriding methods in derived classes. It allows you to ensure that certain logical structures or functionalities are not altered, which can be very important in large projects or when working with external libraries.
Step-by-step guide
To show you how to use the final keyword in PHP, we will walk through a typical use case step by step.
1. Definition of the base class
Start by creating a base class that defines some basic information and methods. Suppose you are working on an online store that manages products.

In this class, we have a method getVKPreis that calculates the selling price based on the purchase price. This method should not be overridden.
2. Using the final keyword
Now we use the final keyword to declare the selling price as a non-overridable method.

By adding final before the method getVKPreis, you prevent other classes from overriding this method. This eliminates the risk of unwanted logic changes in derived classes.
3. Creating derived classes
Now we can create one or more derived classes. These classes might want to extend the general functionality of the product, but not alter the calculation of the selling price.

You can see that both FoodProduct and NonFoodProduct inherit from BaseProduct but cannot override the method getVKPreis. An attempt to do so will result in an error.
4. Understanding error handling
Now try to override the getVKPreis method in a derived class. You will receive an error stating that the method cannot be overridden.
The following error will appear: cannot override final method BaseProduct::getVKPreis(). This clearly shows that PHP is protecting you at this point to maintain the integrity of your base class.
5. Practical application
The use of final is not limited to class methods; you can also declare entire classes as final. This means that these classes can no longer be extended.

By using final at the class level, you ensure that no one can extend your base implementation and potentially cause problems.
6. Best practices and considerations
Keep in mind that while final is a powerful tool, it should be used with caution. Too much restriction can reduce the flexibility of your code. Carefully consider which methods are suitable for using final and which may need to be overridden.
Summary – Using final to secure class methods
By using the final keyword in your PHP class, you can effectively prevent important methods from being overridden by derived classes. This ensures a consistent implementation and helps you keep your system stable and maintainable.
Frequently Asked Questions
What happens if I want to override a final method in the parent class?You will receive an error stating that the method cannot be overridden.
Can I declare an entire class as final?Yes, this prevents other classes from inheriting from this class.
Are there cases where I should avoid final?Yes, if you need flexibility and want to allow for extension for derived classes.