One of the many new features of PHP 8.1 is the ability to declare class constants as final
, so that they can no longer be overridden in child classes. The same applies when constants are defined as final in an abstract classes, or interface; they can’t be overridden by classes extending that abstract or implementing that interface. So class and interface constants can now truly become constant.
You could be forgiven for believing that a constant will always retain the same value throughout, it’s in the name, right? And technically that’s correct. But it all comes down to context, to the class (or interface) where that constant is defined, and its visibility; and when it comes to class inheritance, all bets are off. The value of a constant can be defined at one level within the inheritance tree; but overridden at another level. Technically, it’s a different constant; but having constants with the same name yet different values within a class inheritance tree can certainly create confusion, and potentially lead to awkward bugs. The PHP documentation does note that this can be the case; but perhaps it really should be a warning instead.