What is final keyword in PHP?

The final keyword in PHP

The final keyword can be used for class, class methods, and class constants in PHP. If the final keyword is used before the class then it can be called a final class and if final keyword is used before any method of the class then it can be called a final method. Similarly if final keyword is used before any constant of the class then it can be called a final constant .

The final class

A class declared as final class prevent Inheritance i.e. can not be extended by any other class. If we try to extend the Final class by using any other class then PHP will throw fatal error. The syntax of the final class is given below.

Syntax
final Class className{
}

Now I will give an example of final class.

Example

To understand the final class with an example, I will first create a file named finalClass.php. Within this file I will create a final class named ParentClass and create two methods in it. Here I will create a normal method named getName and a final method named getDetails. However, since class the ParentClass is final in this case, there is no need to make any method final. Then I will create a ChildClass that will extend the ParentClass.

The code is as follows.

<?php
final class ParentClass {
   // A non-final method
   public function getName() {
       echo "getName() method of ParentClass class is called";
   }

   // A non-final method - the final keyword is redundant as the class is final 
   final public function getDetails() {
       echo "getDetails() method of ParentClass class is called";
   }
}

class ChildClass extends ParentClass {
}
// Fatal error: Class ChildClass cannot extend final class ParentClass
?>

Now the output of finalClass.php in the browser would be as follows:
Fatal error: Class ChildClass cannot extend final class ParentClass

The final method

A method in a class declared as final prevents child classes from overriding the method. The syntax of the final method is given below.

Syntax
Class className{
    final function functionName(){
    }
}

Now I will give an example of final method.

Example

For the final method example, I will first create a file named finalMethod.php. Within this file I will create a class named ParentClass and create two methods in it. Here I will create a normal method named getName and a final method named getDetails. Then I will create a ChildClass to extend the ParentClass and within this ChildClass I will try to override the getDetails method.

The code is as follows.

<?php
class ParentClass {
   // A non-final method
   public function getName() {
       echo "getName() method of ParentClass class is called";
   }

   // A non-final method - the final keyword is redundant as the class is final 
   final public function getDetails() {
       echo "getDetails() method of ParentClass class is called";
   }
}

class ChildClass extends ParentClass {
    
    public function getDetails() {
       echo "getDetails() method of ChildClass class is called";
   }
}
// Fatal error: Cannot override final method ParentClass::getDetails()
?>

Now the output of finalMethod.php in the browser would be as follows:
Fatal error: Cannot override final method ParentClass::getDetails()

In the above example if I override the getName method on the ChildClass then I don’t get any error as this is a normal (non-final) method.

The final constant

A constant in a class declared as final prevents child classes from overriding the constant. The syntax of the final constant is given below.

Syntax
Class className{
    final const constantName = "SOMEVALUE";
}
Example

Here I will first create a file named finalConstant.php. Within this file I will create a class named ParentClass and create one final constant named unit. Then I will create a ChildClass to extend the ParentClass and within this ChildClass I will try to override the unit constant.

The code is as follows.

<?php
class ParentClass {
   final public const unit = "kg";
}

class ChildClass extends ParentClass {
    public const unit = "pound";
}
// Fatal error: ChildClass::unit cannot override final constant ParentClass::unit
?>

Now the output of finalConstant.php in the browser would be as follows:
Fatal error: ChildClass::unit cannot override final constant ParentClass::unit

You can learn more about final keyword in PHP at: https://www.php.net/manual/en/language.oop5.final.php