Since PHP 8.1, regular mutable objects can be used as constants via the `const` keyword or `define()`, a side effect of adding enum support. While enum cases are objects and can be constants, this inadvertently allowed any mutable object to be declared as a constant. The object's state can still be changed through method calls, only direct property assignment is blocked by the compiler. The author argues this behavior is unintentional, counter-intuitive, and recommends avoiding constant objects (except enums) until the language addresses the inconsistency.
Table of contents
A short history lessonEnums are classes, and enum cases are objectsDefining constant objectsUsing constant objectsWhat do we take from it?Sort: