What is the final class in php?

What is the final class in php?

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:

display1();
$bmwcar1->price1();
$tata1->display1();
$tata1->price1();
?>

Output:

What is the final class in php?

Example #2

Here the “A1” class is created with a FINAL keyword. Then normal function “show1()” is created with two variables. Inside of it sum of the variables will be stored in $sum1 variable. Then echo function used to print variable values sum. Then “B1” class is created to extend the class “A1”. Inside the “B1” class multiplication of the variables is done and saved in a $multi1 variable. Multiplication result is printed using the echo functionality but here we are extending the final class “A1” with normal class “B1”. Here we extend the FINAL class/methods. It returns FATAL ERROR because we extending a FINAL class.

Code:

show1(1001,1001);
?>

Output:

What is the final class in php?

Example #3

Here a normal class “C1” with the function e($xx1) is created then a class “D1” is created to extend the class “C1” to use “C1” parameters. So class “D1” will inherit “C1” class properties and also will contain the properties “D1” class. Up to this everything is correct. Then a class “A1” is created with the final function/method inside it with “x1”, “y1” variables which calculates the sum of variables. After this “B1” class is created to extend “A1” class. But final classes, methods are non-extendable. If tried FATAL ERROR will occur.

Code:

e(13);
$obj= new B1();
$obj->show(100,100);
?>

Output:

What is the final class in php?

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:

showone(12,12);
$obj->calculateone(10,10);
?>

Output:

What is the final class in php?

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.

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 –

  1. PHP References
  2. PHP Log Errors
  3. PHP Array Search
  4. Abstract Class in PHP

What is a final class?

A final class is a class that can't be extended. Also methods could be declared as final to indicate that cannot be overridden by subclasses. Preventing the class from being subclassed could be particularly useful if you write APIs or libraries and want to avoid being extended to alter base behaviour.

How do you call a final class method in PHP?

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. Note: Properties cannot be declared final: only classes, methods, and constants (as of PHP 8.1. 0) may be declared as final.

What is the meaning of a final class and a final method?

You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final .

Can final class inherited PHP?

A final class can contain final as well as non final methods. But there is no use of final methods in class when class is itself declared as final because inheritance is not possible.