O'Reilly logo

PHP 5 Power Programming by Derick Rethans, Stig Sæther Bakken, Andi Gutmans

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.15. final Methods

Until now, you have seen that when you extend a class (or inherit from a class), you may override inherited methods with a new implementation. However, there are times where you might want to make sure that a method cannot be re-implemented in its derived classes. For this purpose, PHP supports the Java-like final access modifier for methods that declares the method as the final version, which can't be overridden.

The following example is not a valid PHP script because it is trying to override a final method:

class MyBaseClass {
    final function idGenerator()
    {
        return $this->id++;
    }

    protected $id = 0;
}

class MyConcreteClass extends MyBaseClass {
    function idGenerator()
    {
        return $this->id += 2;
    }
}

This script won't work because ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required