-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Deprecate implicit nullable parameters #3535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate implicit nullable parameters #3535
Conversation
I'd recommend a separate RFC for this one. This is a pretty major deprecation which is likely doing to be controversial. It's probably better to handle this separately and early (I don't expect that we'll be voting the bulk-deprecation RFC soon.) |
Alright then. Typed properties RFC states that:
Should this legacy behaviour stay, I think typed properties must support it too. Otherwise it'd be just another candidate for #PHPSadness (inconsistent syntax). I'll work on a separate RFC for this. 👍 |
Part of the motivation of requiring explicit nullable types is technical in nature. There is a somewhat minor but annoying issue relating the the automatic nullability promotion: A null default value can be the result of a constant expression which is not evaluable at compile-time. function test(array $param = VAL) {}
const VAL= null; // Could be in a different file
test(null); This code is valid, because we have special handling to check at runtime whether the default value evaluates to null and treat the parameter as nullable in that case. However, this is imperfect, because it only affects which values the function accepts, but not inheritance checks or reflection. In particular, the following code class A {
function test(array $param = VAL) {}
}
class B extends A {
function test(array $param = VAL2) {}
}
define('VAL', null);
define('VAL2', 'notnull'); is legal, even though it effectively restricts a parameter from This is something you might want to include in your RFC. My own 2c are that it's too early. While I'm in favor of phasing this out long-term, deprecating it now means that it's not possible to write code that is compatible (deprecation-free) with both PHP 7.0 and PHP 7.4, which for me is too restrictive. |
@nikic Deprecations are a good thing to happen during minors, otherwise you'd have to rely on majors to do so. This means we'd only be able to prevent implicit nullability by PHP 9, which is a decade ahead of us. I'd be fine to add deprecation notice on 7.X and remove support by 8.X |
Understood, it may be an issue for some libraries.
|
Comment on behalf of carusogabriel at php.net: Labelling |
I understand the motivation and I personally don't write
So people that write I think that requiring |
another reason to write So if this gets deprecated, I suggest giving time for it (#3535 (comment) might be fine) |
There's a related Twitter thread: https://twitter.com/SaraMG/status/1123624550632017920 |
7d52439
to
6921e48
Compare
6921e48
to
060f6a7
Compare
060f6a7
to
727c62e
Compare
@Majkl578, what's the status here? Did you start an RFC? @ondrejmirtes, see #3535 (comment) for why this isn't just a BC break for the sake of a BC break. :) |
I'll close this for now. It doesn't look like there's an associated RFC either. Feel free to reopen when development continues. |
This PR proposes to deprecate the legacy signature of declaring nullable types by simply using
NULL
default value without explicit?
sign:in favor of:
The former signature is a relic from pre-7.1 era where nullable types didn't exist.
We also had an interesting discussion about this in past, i.e.: doctrine/common#805 (comment)
It also came up on Twitter recently: https://twitter.com/malukenho/status/1042025943873859584
Typed properties (coming in PHP 7.4) explicitly disallow the former declaration of implicit nullable types. I believe it'd be confusing to keep this legacy behavior, supporting it only for parameters and not typed properties - thus this deprecation should also target PHP 7.4.