Final keyword in PHP is used in different context. The final keyword is used only for methods and classes.

Final methods: When a method is declared as final then overriding on that method can not be performed. Methods are declared as final due to some design reasons. Method should not be overridden due to security or any other reasons.
Example:
<?php // Program to understand use of // final keyword for methods class Base { // Final method final function printdata() { echo " Base class final printdata function"; } // Non final method function nonfinal() { echo "\n This is nonfinal function of base class"; } } // Class that extend base class class Derived extends Base { // Inheriting method nonfinal function nonfinal() { echo "\n Derived class non final function"; } // Here printdata function can // not be overridden } $obj = new Derived; $obj->printdata(); $obj->nonfinal(); ?> |
Base class final printdata function Derived class non final function
Final Classes: A class declared as final can not be extended in future. Classes are declared as final due to some design level issue. Creator of class declare that class as final if he want that class should not be inherited due to some security or other reasons. 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.
Example:
<?php // Program to understand final classes // in php final class Base { // Final method final function printdata() { echo "final base class final method"; } // Non final method function nonfinal() { echo "\nnon final method of final base class"; } } $obj = new Base; $obj->printdata(); $obj->nonfinal(); /* If we uncomment these lines then it will show Class Derived may not inherit from final class (Base) class Derived extends Base { } */?> |
final base class final method non final method of final base class
Note: Unlike Java final keyword in PHP can only be used for methods and classes not for variables.
Recommended Posts:
- PHP | var keyword
- $this keyword in PHP
- Node.js decipher.final() Method
- Node.js cipher.final() Method
- JavaScript | new Keyword
- CSS | currentcolor keyword
- What's the yield keyword in JavaScript?
- CSS | revert keyword
- Python in Keyword
- ES6 | Void Keyword
- Difference between revert and unset keyword in CSS
- CSS | unset keyword
- Difference between private keyword and private fields in TypeScript
- JavaScript | Super keyword
- How to bind 'this' keyword to resolve classical error message 'state of undefined' in React?
- Access Super Class Methods and Instance Variables Without super Keyword in Java
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

