PHP has a history of performing various forms of type conversions.
While most dubious cases of such conversions nowadays throw a TypeError
,
or at least emit an E_WARNING
, the conversion of resources to strings
does none of these things.
As a consequence it is possible to concatenate a resource with a string without any errors,
but performing any sort of arithmetics with it throws a TypeError
:
will result in:
string(19) "Resource id #3hello" Fatal error: Uncaught TypeError: Unsupported operand types: resource + int in FILE
This is surprising behaviour, especially as a non-stringable object being used with
the concatenation operator would throw an Error
.
Considering that resources are being phased out in favour of opaque objects
and do not support being converted to strings.
We propose to align the behaviour with that of converting an array
to string
,
which is to emit a warning on implicit and explicit conversions.
Emit an E_WARNING
when a conversion from resource
to string
occurs.
Some common situations where this can occur:
echo
(string)
castAs warnings could be promoted to an exception via an error handler, this may result in exceptions being thrown in place where none previously could.
Next minor version, PHP 8.5.
As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.
Voting started on 2024-XX-XX and will end on 2024-XX-XX.
TypeError