blog

Home / DeveloperSection / Blogs / Final class and method in PHP

Final class and method in PHP

Jayden Bell 2342 22-Jun-2016

In this part we will learn on the following topics:

·         What is final class?

·         When to declare Final class?

·         What is final method?

·         When to declare final method?

What is final class?                  

 In our previous section, of inheritance we have seen how derived class (or child class) inherits the function or methods of base class (or parent class). There can be a situation where you want to prevent a class from being inherited. This can be done by using final keyword preceded by class name. Following is an example which shows how to use final keyword in your class:

Code Implementation:
<?php

final class text { // class text is final
function term() {
echo "hello!! i am parent class.";
}
}
class child extends text
{
function mylife()
{
echo "hello!! i am child class.";
}}
$obj = new text();;
$obj->term();
?>

 In above example, text is a base class and child is a derived class and when derived class tries to inherit the parent class (text) compiler throws an error.

Code output:
Fatal error: Class child may not inherit from final class (text) in c.

 

So, from above example it concludes that final keyword is used to prevent a class from being inherited. In other words, final class can never be inherited.

What is final method?

As in case of inheritance derived class inherits all the methods or function of base class (parent class). There can be a situation where you want to prevent a function or method from being inherited by child class. This can be done by using final keyword preceded by function name. Following is an example which shows how to use final keyword in your method:

Code Implementation:
<?php
 class text {
final function term() {
echo "hello!! i am parent class.";
}
}
class child extends text
{
function term()
{
echo "hello!! i am child class.";
}}
$obj = new text();;
$obj->term();
?>

 As from the above example, you can see when child class inherit parent class method compiler throws error:

Code output:
Fatal error: Cannot override final method text::term() in

So, from above example it concludes that final keyword is used to prevent a class method or function from being inherited. In other words, final method of parent class can never be overridden by child class.

Following is an example to show the correct way to use final keyword:
Code Implementation:
<?php
 class text {
final function term() {
echo "hello!! i am parent class.";
}
 function term1() {
echo "hello!! i am parent class second method.";
}
}
class child extends text
{
 function term1() {
echo "hello!! i am child class second method.";
}}
$obj = new child();
$obj->term();
$obj->term1(); // display the child class method
$obj1 = new text();
$obj1->term1(); // display the parent class method
?>
 Code Output:
hello!! i am parent class.
hello!! i am child class second method.
hello!! i am parent class second method. 
When to use final class?

A class should made final when you want your class should never be changed by derived class or child class. This is done mainly in utility classes where you don’t want the behavior or implementation of your parent class gets changed.

When to use final method or function?

A class function is declared as final when your function contains necessary functionality to support application and any change or modification to your function can cause unexpected errors or bugs.


php php  class 
Updated 15-Mar-2018

Leave Comment

Comments

Liked By