Re: [RFC] Deprecate Fuzzy Type Casts and Allow Stringable in Strict Mode
| From: | Rob Landers | Date: | Tue, 27 Jan 2026 09:36:58 +0000 |
| Subject: | Re: [RFC] Deprecate Fuzzy Type Casts and Allow Stringable in Strict Mode | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-129938@lists.php.net to get a copy of this message | ||
On Fri, Jan 23, 2026, at 11:48, Alexandre Daubois wrote:
> Hello everyone,
>
> Before going further with our already under discussion RFC about
> nullable casts, Nicolas Grekas and I would like to present this new
> RFC about deprecating fuzzy casts, and adding support for Stringable
> in string parameters when using strict mode.
>
> RFC: https://wiki.php.net/rfc/deprecate-fuzzy-casts
>
> Thanks,
>
> — Alexandre Daubois
>
This is interesting. As a user of non-strict mode, I rarely need casts. That being said, after
working on tons of strict-mode code bases, this is exactly the footgun I use to show how strict-mode
is actually bad for their code base: I remove the casts and turn off strict mode, and the
application actually crashes because it was silently handling bad inputs all over the place.
Here's a pretty good example I saw a few weeks ago:
$newBalance = (int)$walletBalance;
$wallet->store($newBalance);
/* ... later ... */
$log->append($walletBalance); // which takes a string
Now, if this string is something like "1000; drop table money" and there isn't proper
injection protection... you're in for a bad day.
Without strict mode:
$wallet->store($walletBalance); // TypeError: cannot coerce string to int
That being said, I think the more proper solution is to simply remove strict mode instead of making
casts stricter. Why? Sometimes you actually do need to use a cast to force a specific type, and you
want to use the loss of information. Like I’m pretty sure casting a float to an integer is faster
than calling floor(), and you end up with an integer, not a float. Secondly, to properly use
strict-mode, you have to religiously cast everything, which hides errors that’d otherwise appear.
But getting rid of lossy casting seems like trying to apply a tourniquet to the wrong limb.
— Rob