What is the benefit of 'Typed Class Constants' introduced in PHP 8.3?
Last updated
Typed Class Constants let you declare an explicit type, e.g.
const string FOO = 'x';, enforced on the value and on subclass overrides.Previously a child class could silently redeclare a constant with a different type. Now mismatches are caught at compile time.
They document intent, improve IDE and static analysis, and work on class, interface, and enum constants.
- https://php.watch/versions/8.3/typed-constants
- https://wiki.php.net/rfc/typed_class_constants
- https://www.reddit.com/r/PHP/comments/11sp9ur/rfc_typed_class_constants/
- https://lindevs.com/use-typed-constants-in-php-8-3
- https://www.php.net/manual/en/language.oop5.constants.php
- https://www.infoworld.com/article/2335565/php-83-brings-typing-of-class-constants.html
- https://stitcher.io/blog/new-in-php-83