Use of PHP final

As like as other programming languages, like, Java, the final keyword is added into PHP as of it’s version 5. This keyword is used as a modifier, but only applicable for PHP classes and functions.

If the final modifier is applied to PHP classes, then, the class cannot be inherited by some other class. At the same time, if this keyword is applied for a function declaration, then, this function cannot be overridden.

PHP final Class

Let us look into the following example, to see how a PHP class with final modifier acts while inheriting it, and there by, to prove the above statement about PHP final class.

php_final

In this example, we deals with two PHP classes, Inc and SubInc, having two functions, postinc and preinc, respectively, to perform post increment and pre increment operation of a PHP variable. And, the Inc class is with final modifier, and the SubInc class is attempt to inherit this class using extends keyword, as we have seen with PHP inheritance. And the complete code is,

<?php
final class Inc {
function postinc($php_count) {
return $php_count++;
}
}
class SubInc extends Inc {
function preinc($php_count) {
return ++$php_count;
}
}
?>

Since Inc is defined as PHP final class, SubInc cannot inherit this class, and so, it leads PHP fatal error like as below.

Fatal error: Class SubInc may not inherit from final class (Inc) in ... on line ...

PHP final Methods

PHP final methods are used to define, to protect themselves from function overriding, and there by preserves their output behavior as expected.

For that, we can change the above program by shifting the final keyword from Inc class to its function postinc. And then, we need to redefine postinc function inside SubInc class. Now the changed code will be,

<?php
class Inc {
final public static function postinc($php_count) {
return $php_count++;
}
}
class SubInc extends Inc{
public static function postinc($php_count) {
return $php_count++;
}
public static function preinc($php_count) {
return ++$php_count;
}
}
?>

While executing this program to check the behavior on overriding final methods, it also cause fatal error as occurred on inheriting  PHP final classes. And the error is,

Cannot override final method Inc::postinc() ... on line ...

final Usage: PHP Vs Other Languages

As like as PHP, in Java, it uses final keyword to restrict a class not to be inherited and also it restricts Java methods not to be overridden. But additionally, Java uses final for defining constants. Since, PHP provides const keyword for class constants and define() method for this purpose, final is used only to obtain two former restrictions.

In C#, final keyword is not used but the same class and method protection is implemented by using a keyword named as, sealed.

Note:

  • In PHP, final keyword is applicable only for classes and methods
  • In PHP final classes, the final keyword will not prevent to access its properties and methods. Instead, can be accessed by using the name of the class or its instance, as usual.

This PHP code tutorial was published on July 3, 2013.

↑ Back to Top