1. Introduction
In PHP Object-Oriented Programming (OOP), the final keyword is used to restrict inheritance or method overriding. It helps developers protect certain classes or methods from being modified by child classes.
This feature is useful when developers want to maintain the original behavior of a class or method and avoid accidental changes by subclasses.
The final keyword can be applied to:
- Classes
- Methods
Note: The final keyword cannot be applied to variables in PHP.
- In PHP, variables cannot be declared as final.
- If you want to define a fixed value, you should use the const keyword.
- In some programming languages like Java, the final keyword can be used for variables, but in PHP it is mainly used for classes and methods.
Note: Using the final keyword helps maintain data integrity, code stability, and security in applications.
2. Syntax of Final Keyword
The final keyword can be used with both classes and methods.
Syntax for Final Class
final class ClassName {
}
Note: A class declared as final cannot be extended by any other class.
Syntax for Final Method
class ClassName {
final function functionName() {
}
}
Note: A method declared as final cannot be overridden in a child class.
3. Final Class in PHP
A final class is a class that cannot be inherited by another class. This means no other class can extend it.
Final classes are often used when developers want to protect important functionality from modification.
Example of Final Class
final class Communication {
public function language() {
return "Speak usually Hindi language.";
}
}
class Indian extends Communication {
public function showInformation() {
echo $this->language();
}
}
$ind_obj = new Indian;
$ind_obj->showInformation();
Output
Explanation
- We created a class named Communication.
- The class is declared as final.
final class Communication
3. Then we tried to extend it using another class named Indian.
class Indian extends Communication
Since Communication is a final class, PHP does not allow inheritance.
Therefore, the system generates a fatal error.
This ensures that the original class cannot be modified or extended.
4. Final Method in PHP
A final method is a method that cannot be overridden in a child class.
However, the method can still be inherited and used by the child class.
Example of Final Method
class Communication {
final public function language() {
return "Speak usually Hindi language.";
}
}
class Indian extends Communication {
public function language() {
return "Speak usually English language.";
}
public function showInformation() {
echo $this->language();
}
}
$ind_obj = new Indian;
$ind_obj->showInformation();
Output
Explanation
In this example:
1. We created a class Communication.
2. Inside this class, we declared a method as final.
final public function language()
- Then we created a child class Indian that extends Communication.
- In the child class, we tried to override the same method.
public function language()
Since the method is declared as final, PHP does not allow overriding.Therefore, the system throws a fatal error
5. Can Final Method Be Inherited?
Yes. A final method can be inherited by child classes, but it cannot be overridden.
This means the child class can use the method exactly as it is defined in the parent class.
Example
class Communication {
final public function language() {
return "Speak usually Hindi language.";
}
}
class Indian extends Communication {
public function people() {
return "Indian people ";
}
public function showInformation() {
echo $this->people() . $this->language();
}
}
$ind_obj = new Indian;
$ind_obj->showInformation();
Output
Explanation
In this example:
- The language() method is declared as final in the parent class.
- The child class Indian does not override the method.
- Instead, it simply uses the inherited method.
The child class combines its own method people() with the inherited method language().
This shows that final methods can be used by child classes but cannot be modified.
6. Real-World Example
Consider a Payment System where the payment verification method should never be changed by subclasses.
Example
class Payment {
final public function verifyPayment() {
return "Payment verified successfully.";
}
}
class CreditCardPayment extends Payment {
public function processPayment() {
echo $this->verifyPayment();
}
}
$payment = new CreditCardPayment;
$payment->processPayment();
Output
Explanation
In this example:
- The method verifyPayment() is declared as final.
- This ensures that subclasses cannot change the payment verification logic.
- However, they can still use the method.
This protects critical parts of the system.
7. Advantages of Final Keyword
Using the final keyword provides several benefits
1. Prevents Method Overriding
It ensures that important methods cannot be changed in child classes.
2. Improves Security
Critical functionality remains protected from accidental modification.
3. Maintains Code Stability
Developers can ensure that certain parts of the program behave consistently.
4. Improves Code Design
It clearly defines which classes or methods are not intended for inheritance.
8. Difference Between Final Class and Final Method
| Feature | Final Class | Final Method |
| Inheritance | Cannot be extended | Can be inherited |
| Overriding | Not applicable | Cannot be overridden |
| Purpose | Prevent subclassing | Protect specific methods |
10. Conclusion
The final keyword in PHP is used to restrict inheritance and method overriding in Object-Oriented Programming. It helps developers protect important classes and methods from being modified by child classes.
A final class cannot be extended, while a final method cannot be overridden but can still be inherited.
PHP Final keyword – Interview Questions
Q 1: What is the final keyword in PHP?
Q 2: Can a final class be extended?
Q 3: Can a final method be overridden?
Q 4: Why use final keyword?
Q 5: Is final used for security?
PHP Final keyword – Objective Questions (MCQs)
Q1. final keyword prevents ______.
Q2. Which is valid use of final?
Q3. A final class cannot be ______.
Q4. A final method cannot be ______.
Q5. Final keyword helps in ______.