On 02.06.2025 at 18:11, Gina P. Banyard wrote:

> The RFC proposes to deprecate implicit type coercions to and from the bool 
> type for other scalar types.
> This a "weak" mode change only, as when strict_types are enabled none of 
> these coercions can happen.
> 
> Let me know what you think about it.

Let's consider a legacy (PHP 5) function:

    function foo($bool)
    {
        if ($bool) {
            // something
        } else {
            // something else
        }
    }

Now the developer wants to add the missing parameter type declaration.
As is, even without static analysis or a test suite, they can just add
`bool`, being reasonable sure that the code behaves like before for
scalar types, and only fails for non-scalars, something they may be fine
with.  With the suggested change, they can no longer, but would rather
have to inspect and possibly "fix" all callers.  Since the latter may
not be possible (perhaps because it is an API function), they would just
stick without type declaration, or use the scalar union type.  In my
opinion, either is worse than the current type coercion behavior.

Now, one may argue that there should be no boolean parameters at all,
but besides that this is opiniated, maybe it's the reason why the
developer wants to add the type declaration: to be quickly able to
determine boolean parameters, and to move away from those over time.

So, besides that the proposed change would make it harder to bring
legacy code up to date, it likely breaks a ton of code which already
uses `bool` type declarations.  That BC break alone is a show-stopper
for me.

Generally, I think that any attempt to unify strict and coercive typing
is futile – that ship has sailed long ago.  Those preferring strict
typing likely would not want to compromise, and those preferring
coercive typing will be annoyed by being forced into stricter behavior.

Reply via email to