Re: [RFC] [Vote] Deprecate functions with overloaded signatures

From: Date: Mon, 03 Jul 2023 13:29:05 +0000
Subject: Re: [RFC] [Vote] Deprecate functions with overloaded signatures
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-120738@lists.php.net to get a copy of this message
On Thu June 29, 2023 at 11:51 AM Theodore Brown wrote: > On Tue, June 27, 2023, 6:33 Rowan Tommins wrote: > > On 27/06/2023 02:25, Theodore Brown wrote: > > > Currently the following code returns only the array keys that loosely equal null: > > > > > > array_keys($array, null) > > > > > > If the function is changed to a single signature with $filter_value defaulting to > > > null (with an argument count check to preserve the above functionality), its behavior becomes a lot > > > more surprising/unexpected. If a parameter defaults to null, as a user I don't expect the > > > function to work differently when I explicitly pass the default value. > > > > > > An alternative solution to this situation is to introduce a new dummy > > value for such parameters, similar to how JavaScript uses the special > > "Symbol" type to have keys that can't collide with any userland value. > > In this context, this could be achieved with a single-value enum, i.e.: > > > > enum ArrayKeys { case IncludeAll; } > > > > function array_keys(array $array, mixed $filter_value = > > ArrayKeys::IncludeAll, bool $strict = false): array {} > > > > That way, the optional parameter has a real default value, distinct from > > any value the user might wish to filter by, including null, and full > > compatibility is maintained with existing code. > > ...Note that currently there is a draft RFC to support objects in array keys. [1] If that > feature is accepted, would it not be allowed to filter array keys by the > ArrayKeys::IncludeAll enum value? That would also be > surprising/unexpected. I must have been really tired when I wrote the above paragraph, because I forgot that the filter_value parameter matches against the value, not the key (so the Object Keys in Arrays RFC has nothing to do with it). But this makes using an enum as the default filter_value even more problematic, since enums can already be used as array values and this would prevent filtering an array to return keys for this value. I.e. the following code would stop working: https://3v4l.org/5Br1a So I still believe the overloaded array_keys signature should be deprecated for consistency and to avoid confusing behavior. Best regards, Theodore [1]: https://wiki.php.net/rfc/object_keys_in_arrays

« previous php.internals (#120738) next »