Final Keyword in PHP

PHP 5 introduced the final keyword, which is used to restrict certain behaviors in PHP. It can be applied either to an entire class or to specific methods within a class. You cannot apply final keyword with properties in PHP.

The final keyword has two main uses in PHP:

  • To prevent inheritance – When a class is declared as final, it cannot be extended. No other class can inherit from it.
  • To prevent method overriding – When a method is declared as final, no child class can override that method, although it can still inherit and use it.

Final Class in PHP


When you declare a class using the final keyword, it is called a final class in PHP. A final class cannot be extended, which means it prevents the class from being inherited by any other class.

PHP will generate a fatal error if you try to extend a class marked with final keyword. You can mark a class final by using final keyword in the front of its definition. The basic syntax to declare a final class in PHP is:

final class ClassName {
   // class code
}

Example 1: Valid Usage of Final Class

<?php
final class Calculator {
   public function add($a, $b) {
     return $a + $b;
   }
}

$calc = new Calculator();
echo $calc->add(10, 20);
?>

Output:

30

In this example:

  • We have declared the class Calculator as final.
  • The final class contains a method named add() with two parameters, $a and $b.
  • Since final keyword does not restrict object creation, we have created an object of class Calculator.

Example 2: Trying to Extend a Final Class (Invalid Usage)

<?php
final class ParentClass{
   public function msg(){
     echo "ParentClass method";
   }
}
class ChildClass extends ParentClass {

}
?>

Output:

Fatal error: Class ChildClass cannot extend final class ParentClass

In this example:

  • We have declared a class named ParentClass as final.
  • ChildClass attempts to extend the final class ParentClass.
  • Since ParentClass is declared as final, it cannot be extended, which prevents inheritance at compile time.
  • As ChildClass extends a final class ParentClass, PHP immediately throws a fatal error.

Final Method in PHP


When you declare a method using the final keyword, it is called a final method in PHP. A final method cannot be overridden in a child class. It simply prevents the method from being redefined in child classes. The basic syntax of final method is:

class BaseClass {
   final public function methodName(){
     // Body of the final method
   }
}

In the above syntax:

  • The class BaseClass contains a method declared with the final keyword.
  • Once a method is declared as final in the parent class, any child class that declares a method with the same name will cause a fatal error.
  • The final method can be inherited, but cannot be overridden in a child class.

Example 3: Inheriting a Final Method (Valid Case)

<?php
class ParentClass {
   final public function showMessage() {
      echo "This is a final method.";
   }
}
class ChildClass extends ParentClass {
   public function display() {
      $this->showMessage(); // Allowed
   }
}
$obj = new ChildClass();
$obj->display();
?>

Output:

This is a final method.

In this example:

  • The ParentClass contains a method showMessage() declared as final.
  • The child class ChildClass extends ParentClass, but does not override the inherited final method.

Example 4: Overriding a Final Method (Invalid Case)

<?php
class ParentClass {
   final public function greet() {
      echo "Hello, this is a parent class method.";
   }
}
class ChildClass extends ParentClass {
   public function greet() {
     echo "Hello, this is a child class method."; // Not allowed
   }
}
?>

Output:

Fatal error: Cannot override final method ParentClass::greet()

In this example:

  • The method greet() is declared as final in ParentClass.
  • ChildClass extends the ParentClass, which tries to override the inherited final method.
  • Since a method marked with final keyword cannot be overridden inside the child class, PHP immediately throws a fatal error.
  • Overriding a final method is strictly prohibited in PHP.

Final Constants in PHP


Unlike Java programming, you cannot use the final keyword for class constants in PHP. You can use const keyword for this. For example:

class College {
   const collegeName = "PIET";
}

From PHP 7.1 onwards, class constants can have visibility modifiers, such as public, protected, private, but they still do not use final.

Key Points to Remember About Final Keyword:
  • The final keyword is applicable only to classes and methods.
  • Subclasses can inherit final methods, but they cannot override them.
  • A class declared as final cannot extend.
  • A final class cannot contain abstract methods, because abstract methods require inheritance for implementation, and a final class cannot be inherited.
  • You can also declare static methods as final.
  • Final keyword cannot be used with properties or constants.
  • You can create objects of a final class normally.

 

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the founder of Scientech Easy and a passionate coding educator with 8 years of professional experience in Java, Python, web development, and core computer science subjects. With expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He consistently publishes in-depth tutorials, practical coding examples, and valuable learning resources for beginners as well as working professionals. Each article is carefully researched and reviewed to ensure accuracy, quality, and real-world relevance.