PHP 8.5: Non-canonical scalar type casts (boolean|double|integer|binary)
deprecated
PHP's scalar type casting permitted some variations of scalar types. For example, it is possible to cast a variable to an integer using both (integer)
and (int)
casts:
$value = '42';
(integer) $value; // int(42)
(int) $value; // int(42)
PHP scalar types have these alternate variations:
Type | Canonical type name | Alternative type name |
---|---|---|
Integer | (int) |
(integer) |
Float | (float) |
(double) |
Boolean | (bool) |
(boolean) |
String | (string) |
(binary) |
The alternate type names are not supported in parameter, return, or property types:
function test(integer $value): double {}
Warning: "double" will be interpreted as a class name. Did you mean "float"? Write "\double" to suppress this warning in ... on line ...
Warning: "integer" will be interpreted as a class name. Did you mean "int"? Write "\integer" to suppress this warning in ... on line ...
Due to this inconsistency, PHP 8.5 deprecates the four alternative scalar type names listed above.
In PHP 8.5 and later in the PHP 8.x series, using the deprecated casts emits a deprecation notice:
(integer) $value
Deprecated: Non canonical cast (integer|boolean|double|binary) is deprecated, use the (int|bool|float|string) cast instead
In PHP 9.0 and later, PHP will no longer recognize (integer|boolean|double|binary)
casts, and will consider them as a cast to a class with the same name instead.
Recommended replacement
To avoid the deprecation, replace the cast to the canonical scalar type:
- (integer) $value; // int(42)
+ (int) $value; // int(42)
- (double) $value; // float(42)
+ (float) $value; // float(42)
- (boolean) $value; // bool(true)
+ (bool) $value; // bool(true)
- (binary) $value; // string(3) "foo"
+ (string) $value; // string(3) "foo"
Backward Compatibility Impact
This change introduces a new deprecation notice for each cast operation of the alternate scalar types. This is emitted at the time of execution; not at the time of compilation.
Replacing the deprecated scalar type names with the canonical labels is a backward-compatible fix.