Hi Craig,

Don't get me wrong, but I have a feeling you still didn't understand fully
my first message.

That's what I thought, but for some reason, casting null is deprecated only
> when it's provided as an argument to functions, all other cases it's
> fine... weird right?
>

No, passing null to non-nullable parameters is already an error. It has
been for a long while now. The only exception are some built-in functions
that silently ignored that and cast the value to another type.


> > Instead, if you want, submit a proposal to reverse the direction.
>
> No point, people will vote against, and will just entrench their position
> (people like to think they are right, this would involve them changing
> their mind; and because everyone voting is almost certainly using
> strict_types=1, it won't affect them, and they will see it as a way of
> pushing "lesser developers" to using strict types).
>

I don't use strict types and I am pretty sure there are many more people
that don't. I started using them more recently but I don't use them
everywhere yet. However this discussion doesn't have much to do with strict
types.

> Such a change could also be complicated to implement given how ambiguous
> it would be.
>
> It always worked that way (for internal functions) - for arguments that
> accept a string, cast null to an empty string; if it accepts an integer
> then it's 0, etc.
>

It only worked like that for some built-in functions. That's because
built-in functions didn't (and some still don't) respect the PHP type
system. However, passing null to non-nullable param was never allowed.


> So unless it's causing some serious problems for PHP developers or users,
> I think it's too late to change the design of the language.
>
> At the moment it's causing deprecation log entries (which are ignored),
> but the problems are everywhere, like the following (Laravel), is this
> really that unreasonable?
>
> ```
> $a = trim($request->input('a'));
> ```
>
> This will start getting Fatal Type Errors in 9.0... and finding these
> "mistakes" is close to impossible (phpstan doesn't find them; psalm
> requires high levels 1-3).
>

It's only throwing a deprecation because we started to fix the built-in
functions so they behave like the rest of the language. That's a bug fix,
but as I explained earlier it was always an error. That's how the language
was designed.

Finding it is difficult, I agree, but that's the whole purpose of the
deprecation. It's supposed to help you find all the mistakes. The mistakes
are only with built-in functions, so the scope is very limited. With all
other functions the error is caught during development because of the
error.

Casting null to other types is not a mistake, but passing null to functions
that don't expect it, is a mistake. There's no defined behavior in PHP as
to what should happen. Allowing passing null to non-nullable parameters is
a big feature request that never was part of PHP. It's not something that
was removed, it's something that never existed. Just think about a simple
example and how ambiguous it would be.

function abc(string|int $p) {}
abc(null);

What should null be converted into and why? If you as a developer know then
you can cast the argument to an appropriate type but on the language level,
it's not really possible.

So, in conclusion, we are not supporting you because we are stubborn or
that we want to force strict types, but because resolving this problem in a
way you want is extremely difficult and borderline impossible.

Regards,
Kamil

>

Reply via email to