The reason are:
Declaring a class as final prevents it from being subclassed—period; it’s the end of the line.
Declaring every method in a class as final allows the creation of subclasses, which have access to the parent class’s methods, but cannot override them. The subclasses can define additional methods of their own.
The final keyword controls only the ability to override and should not be confused with the private visibility modifier. A private method cannot be accessed by any other class; a final one can.
—— quoted from page 68 of the book PHP Object-Oriented Solutions by David Powers.
For example:
final childClassname extends ParentsClassname {
// class definition omitted
}
This covers the whole class, including all its methods and properties. Any attempt to create a child class from childClassname would now result in a fatal error.
But,if you need to allow the class to be subclassed but prevent a particular method from being overridden, the final keyword goes in front of the method definition.
class childClassname extends parentClassname {
protected $numPages;
public function __construct($autor, $pages) {
$this->_autor = $autor;
$this->numPages = $pages;
}
final public function PageCount() {
return $this->numPages;
}
}
In this example, none of them will be able to overridden the PageCount()
method.