Definition of PHP Final Class
PHP final class is a class which prevents overriding a method of the child classes just by the final prefix with the definition. It means that if we are defining a method with the final prefix then it is going to prevent overriding the method. Usually, the inheritance of the PHP OOPs concept is going to allow the enormous flexibility within the class hierarchy. You can create one or more subclasses which are used to extend the base class’s functionality, but the OOPs concept of PHP gives a way to create non-extendable classes. Such class/classes is/are called Final Class/Final Classes.
Syntax:
final class Pavan{ }
final public function set_bike[]{}
How PHP Final Class Works?
PHP Final Class works based on the declaration just by adding the FINAL keyword before the class word. Final Class works without extending the class. Child class can’t override the final methods. Normal class variables cannot be used to declare as final. PHP Final class or method can’t be extended just like the other normal classes. If the Final class is extended using any other normal class then Final Class will lead the PHP to return FATAL ERROR in the browser output. Likewise the FINAL METHOD too.
Examples of PHP Final Class
Following are the examples given below:
Example #1
Here a class is created using the final keyword before the class. This final class “Automobile1” cannot be extended. Inside the final class, final methods/functions are created. Then the normal class “Bmwcar1” is created with a method/function and then created the final method/function inside paragraph tags. Then a class “Tata1” is created to extend “Bmwcar1” which has the normal function/method “display[]” but the price[] method is not created in the Tata1 class because calling it will provide fatal error because of the final keyword. It is non-extendable. Then the “$bmwcar1” object is made/created. Calling display[] methods and final method’s price[] is done from “Bmwcar1” and “tata1” class.
Code:
Output:
Example #4
In the below example, a normal class is created at first and then a final method is created inside of it with the two variables in order to calculate the sum of the two variable values then a new method/function is created inside of the normal class to multiply the two variable values and returns the result. Here only a final method is created and called without even extending so there will be no occurrence of error/errors. The sum of the variables and also the multiplication of the variables will be printed.
Code:
Output:
Advantages
PHP Final Class concept is unique and non-extendable like the normal classes. Just like the Final Classes, Final methods are also not extendable in a unique way. PHP Final Class concept will prevent massive inheritance functionality and also encourages the composition. All Public APIs of Final Class is/are the part of the interface/interfaces. It helps in encapsulation. The final class/method will prevent the class/method from overriding the child classes/methods. PHP Final Class/classes will prevent exploitation. Even though the final class/classes can’t be extended but these final classes are extendable at some conditions. It is like the Final class becoming or can become into a subclass but it can’t be turned into a superclass.
PHP final keyword can even be placed just before or just after the access specifier. PHP final method/methods also should have no specific/specified return type/types and the method name/names.
Recommended Articles
This is a guide to PHP Final Class. Here we discuss the definition and working of php final class along with different examples and its code implementation. You may also look at the following articles to learn more –
- PHP References
- PHP Log Errors
- PHP Array Search
- Abstract Class in PHP