Multiple Const Types¶
Class constants may be typed since PHP 8.3. Union types are possible, and they are valid with class constants.
In the first case, two constants are declared with different types. PHP complains if any of the types does not apply to the values, so the union type is needed to ensure that both constant are well declared. Obviously, they should be declared on distinct const expressions.
In the second case, the constant is defined with a condition. Now, rand() cannot be called in that part of the code, but using other constants, native such as PHP_OS, __FILE__, custom, global or define() -d may impact the expression, and lead to different results and types. This is a theoretical case, and it has never been seen in the wild.
See Also¶
Multiple Const Types [Try me]