Open In App

What are the final class and final method in PHP ?

Last Updated : 28 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see what is final class & final method in PHP, along with knowing their implementation through the examples.

The terms class, object, method, and final comes under object-oriented programming. In object-oriented programming, there is an important concept called Inheritance that allows one class to inherit properties of another class and even have the capability to override the inherited methods, in order to replace the original behavior given. There are some cases exists where we need to prevent the class from being inherited or from being overridden. For this, the final keyword can be utilized to accomplished  the task in PHP that prevents any class, or method to inherit and override the functionality. It is used simply by prefixing any method or class with the final keyword.

final class: The final class is a class that cannot be extended by other classes. So a Class that is declared with the final keyword doesn’t have child classes. A class can be declared as final by prefixing the final keyword before the Class. The syntax for defining the final class is given below:

Syntax:

final Class className

final method: A method is considered a final method if it is prefixed with the final keyword. The final methods are the methods that cannot be overridden. So, these methods can’t be overridden in child/subclasses. It increases security by preventing the modification of functions. The syntax for defining a final method is given below:

Syntax:

final function functionName(Parameter1, Parameter2, ...);

We will understand these concepts through the below examples.

Example 1: The below example throws an error. Because here the class ParentGFG is declared as final. So any class cannot inherit the classes that are defined as final. So here ChildGFG class cannot inherit the ParentGFG final class. So PHP won’t create a child class for ParentGFG class.

PHP




<?php
  final Class ParentGFG{
     function print(){
      echo "Parent class";
    }
  }
  class ChildGFG extends ParentGFG{
    function print(){
        echo "Child class";
    }
  }
  $object= new ChildGFG();
  $object->print();
?>


Output:

Fatal error: Class ChildGFG cannot extend final class ParentGFG in 
            /home/522fb47d933747216c6e5fe0e51ac10d.php on line 11

Example 2: The below example represents that any function defined as final in the parent class cannot be overridden by its child classes. The ParentGFG class has a method print() which is declared as final. So, whenever any child class of ParentGFG like ChildGFG tries to override the print() method, PHP throws an error.

PHP




<?php
  Class ParentGFG{
     final function print(){
      echo "Method in parent class";
    }
  }
  class ChildGFG extends ParentGFG{
    function print(){
        echo "Overrides parent print method";
    }
  }
  $object= new ChildGFG();
  $object->print();
?>


Output:

Fatal error:Cannot override final method ParentGFG::print() 
            in /home/c055b5cf6357c8cee93036f29c4c2eb8.php on line 11

Advantage of a final class and final method:

  • The advantage of declaring a class or method as final is it increases the security of class and methods by preventing overriding. Both are non-extendable.

Disadvantage of a final class and final method:

  • The disadvantage of declaring a class final is it doesn’t allow to create a child class for that final class thereby decreasing the code reusability.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads