Re: Allowing NULL for some internal functions

From: Date: Wed, 02 Mar 2022 14:39:25 +0000
Subject: Re: Allowing NULL for some internal functions
References: 1 2 3 4 5 6 7 8 9 10 11 12 13  Groups: php.internals 
Request: Send a blank email to internals+get-117210@lists.php.net to get a copy of this message
On 02.03.22 15:00, Craig Francis wrote:
On Wed, 2 Mar 2022 at 12:26, Dik Takken <dik.takken@gmail.com> wrote:
So, to get this crystal clear, this is my understanding of what you are proposing for passing null to a non-nullable function parameter (hopefully my ASCII art will come through ok):
which     | strict_types  | PHP 8.0    | PHP 8.1    | PHP 9
----------|---------------|------------|------------|----------
user      | 1             | TypeError  | TypeError  | TypeError
internal  | 1             | TypeError  | TypeError  | TypeError
user      | 0             | TypeError  | TypeError  | coerced
internal  | 0             | coerced    | Deprecated | coerced
Is this correct?
Yes, that's correct... although I'd be doing this for 8.2, to avoid TypeError exceptions being introduced for non strict_types scripts in 9.0. This is based on the feedback from the quiz and here; so type coercion from NULL would continue to work, like coercion from the other variable types (string/int/float/bool).
Type coercion already often does not work - giving the string "s" to an integer-typed argument will lead to a TypeError, it will not be coerced. I would prefer less coercions rather than more. That you can give a bool-typed argument "s" and it will be coerced to true seems quite bad to me, for example. I am someone who never uses strict_types although I use types everywhere in my code - accepting the string "1" for an integer type seems fine to me, especially because with HTTP requests you always get strings, and often you also get strings from other external sources, like CSV, databases, etc. So type coercion from strings to a certain degree seems reasonable to me (although even there I am using more and more explicit casts), but having to coerce null seems always avoidable to me, and null is a special value explicitely. In my data null and an empty string sometimes are two possible values, and automatic conversion seems like a value judgement by the language that they are kind of similar, which they should not be.

« previous php.internals (#117210) next »