Final abstract class in PHP? Final abstract class in PHP? php php

Final abstract class in PHP?


You could make it final and give it a private constructor:

final class NoticeTypes {  const ERROR = "error";  const WARNING = "warning";  const INFO = "info";  const SUCCESS = "success";  static function getAll() {    $oClass = new ReflectionClass(__CLASS__);    return $oClass->getConstants();  }  private function __construct() {}}

Here, "final" takes care for the "cannot be extended" requirement, while the private constructor takes care of "cannot be instantiated".

As for the "why" you cannot do it, it is just because such is the language specification; also, as @CD001 points out in his comment:

The whole point of abstract classes is that they are to be extended so abstract final is sort of a contradiction

There was actually an RFC to change that, but it seems it didn't make it.