Re: Warn when declaring required parameter after optional one
| From: | Bob Weinand | Date: | Thu, 09 Jan 2020 13:41:34 +0000 |
| Subject: | Re: Warn when declaring required parameter after optional one | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108054@lists.php.net to get a copy of this message | ||
Hey,
While I generally agree that it likely is a bug in new code, I would rather deprecate it than warn
or even error: the change would make it impossible to retain a type without warning while preserving
compatibility with an old PHP version and making incremental migrations harder (would then not be
possible to write warning-free code running on 7.0 and 8.0 at the same time while retaining type
information).
I would like to see it deprecated and then removed in 4+ years.
Bob
> Am 09.01.2020 um 13:27 schrieb Nikita Popov <nikita.ppv@gmail.com>:
>
> Hi internals,
>
> I've created https://github.com/php/php-src/pull/5067 to
> make code like
>
> function test($foo = null, $bar) {}
>
> throw a warning:
>
> Warning: Required parameter $bar follows optional parameter
>
> Historically, having an "optional" parameter before a required one was
> useful for poor man's nullable types. That is, one could write
>
> function test(FooBar $param = null, $param2)
>
> to get an effective
>
> function test(?FooBar $param, $param2)
>
> signature on old PHP versions that did not have native support for nullable
> types.
>
> Since nullable types have been available since PHP 7.1, having a required
> parameter after an optional one is increasingly likely a bug rather than an
> intentional workaround, so I think it would be good to throw a warning for
> this case.
>
> What do you think?
>
> Nikita