Use of PHP final

by Vincy. Last modified on July 3rd, 2022.

As like in other programming languages, like, like Java, the final keyword is added to PHP as of its version 5. This keyword is used as a modifier but is 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 a final modifier acts while inheriting it, and thereby, prove the above statement about PHP final class.

In this example, we deal with two PHP classes, Inc and SubInc, having two function, postinc and print, respectively, to perform post-increment and pre-increment operations of a PHP variable. And, the Inc class is with final modifier, and the SubInc class is an 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, and protect themselves from function overriding, and thereby preserve 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 causes the fatal errors 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

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

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

Note:

  • In PHP, the 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.
Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page