Cast In Const
<?php
class X {
const T = (int) 1.73;
// Fatal error
//const U = (object) [];
}
echo X::T; //
TIL that constant values accepts casting since PHP 8.5. It is possible to cast a value to another type when defining the global or class constant.
This might be most useful when a constant must be available in several flavor, rather than with literal values.
The only cast that is not supported is (object).
See Also
- cast in constant [Try me]
PHP Error Messages
PHP Features
Last updated: 10 September 2026