Re: [VOTE] array_key_first(), array_key_last(), array_value_first(),array_value_last()
| From: | Yasuo Ohgaki | Date: | Sun, 15 Jul 2018 01:24:16 +0000 |
| Subject: | Re: [VOTE] array_key_first(), array_key_last(), array_value_first(),array_value_last() | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-102829@lists.php.net to get a copy of this message | ||
On Thu, Jul 12, 2018 at 8:05 AM Andrea Faulds <ajf@ajf.me> wrote:
> Hi,
>
> CHU Zhaowei wrote:
> > I voted no.
> > I don't think we have an agreement on dealing with non-existing value,
> and the way this RFC proposed, just returning null without any
> notice/warning, is wrong IMO. I know we already do this in other array_*
> functions, but we cannot keep making mistakes just because we already made
> same mistake.
> >
> > Regards,
> > CHU Zhaowei
> >
>
Since PHP supports null coalesce, it makes sense applying null coalesce by
default.
Many users do it for inputs. e.g $var = $_GET['var'] ?? null;
>
> Hmm. Returning null with no warning makes perfect sense for keys, since
> null is not a valid key so there's no ambiguity, but for values it seems
> problematic. On that ground I've decided to change my vote to No for the
> value functions, but keep the vote for Yes for the key functions.
> Someone who wants such behaviour could always do
> ($array[array_key_last($array)] ?? null), I guess.
>
When array_value_last($array) is needed, users would want null coalesce
almost always.
When user don't want it, they can
if (array_key_last($array) === NULL) { // Or if (!count($array)) {
die("hey, no value defined");
}
$var = array_value_last($array);
This usage would be rare and use of ($array[array_key_last($array)] ??
null) for null coalesce is pain.
It lacks API consistency as well.
Therefore, I voted "yes" for both.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net