No Cast In Constant Expressions¶
<?php
class x {
const string A = '42';
const int B = +self::A;
// This, below, is a compile error
const int C = (int) self::A;
}
Constant expressions do not accept the cast operator.
Constant expressions are expressions defining constants and default values. They allow a subset of PHP features, as they are available before the execution phase. For example, they accept operators, such as addition and concatenation. Yet, they do not accept the cast operators, even though they might be emulated in another way. Here, the + operator creates the integer, from another string.
See Also¶
PHP Error Messages¶
PHP Features¶
Last updated: 14 July 2026