Re: Optional parameters without initializer
| From: | Christoph M. Becker | Date: | Tue, 27 Nov 2018 17:41:04 +0000 |
| Subject: | Re: Optional parameters without initializer | ||
| References: | 1 2 3 4 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969387095@lists.php.net to get a copy of this message | ||
On 27.11.2018 at 16:09, Nikita Popov wrote:
> On Tue, Nov 27, 2018 at 3:29 PM Christoph M. Becker <cmbecker69@gmx.de>
> wrote:
>
>> On 27.11.2018 at 14:37, Nikita Popov wrote:
>>
>>> On Tue, Nov 27, 2018 at 1:56 PM Christoph M. Becker <cmbecker69@gmx.de>
>>> wrote:
>>>
>>>> Any ideas how to specify non applicable parameter defaults?
>>>
>>> We should use = null, and additionally make sure that this is really
>>> accepted as a way to specify the default in the implementation (it's not
>>> always the case, as it may require an explicit "!" in zpp).
>>
>> Well, I'd really appreciate fixing the implementation, but that might
>> cause subtle BC breaks, for instance, if an explicitly passed NULL was
>> internally converted to a falsey value with some reasonable resulting
>> behavior, but now the NULL would cause different behavior. And there
>> are some cases such as call_user_func() which do really have no default.
>>
>> Anyhow, the docs are currently supposed to document behavior as of PHP
>> 5.0.0, so we'd need respective changelog entries.
>>
>> Either way, this may be quite some work, since I was able to find 1289
>> files in en/reference with missing initializers for optional parameters.
>> O_o
>
> What I had in mind with the zpp comment are cases where we currently
> error/warning if null is passed. But you are right that in some cases null
> may be reinterpreted as 0. Usually it's fine to fix such cases (and we have
> done this in the past), but of course it requires careful review. I still
> think that this is the "right" way to fix the problem, if we are not in a
> hurry.
There are certainly functions which error/warn if NULL is passed (for
instance, array_filter(…, NULL)), but I'm afraid many functions silently
convert the NULL to a falsey value of the expected type. And there are
a lot of functions which dispatch on the number of passed arguments.
> For call_user_func() in particular, long term this should switch to using
> the variadic function syntax that PHP nowadays supports, that is
>
> call_user_func ( callable $callback, mixed ...$parameter ) : mixed
>
> or possibly
>
> call_user_func ( callable $callback [, mixed ...$parameter] ) : mixed
>
> But I guess that would require adding support for variadics to PhD first :)
I agree, but for the time being
call_user_func ( callable $callback [, mixed $... ] ) : mixed
might be good enough. I can't see a single reason to document an
additional optional $parameter as “Zero or more parameters to be passed
to the callback.“
Finally, if we're able to document default values for all optional
parameters, we may get rid of the brackets. I find them unnecessarily
hard to read.
--
Christoph M. Becker