Re: RFC: Marking return values as important (#[\NoDiscard])
| From: | Volker Dusch | Date: | Thu, 20 Feb 2025 15:54:29 +0000 |
| Subject: | Re: RFC: Marking return values as important (#[\NoDiscard]) | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-126466@lists.php.net to get a copy of this message | ||
> On Wed, Feb 12, 2025 at 9:57 PM Rob Landers <rob@bottled.codes> wrote:
Hey Rob,
Sorry for writing the mail again, I just noticed I forgot to include the
list on my first reply to you, also corrected a mistake in the second
paragraph.
> There will eventually be a php 9, where BC changes will be possible.
I don't assume PHP will change the behavior of widely used core functions
to throw exceptions in PHP 9 as the BC impact will be colossal, hurting
adoption. Or am I misunderstanding you here?
The point that there might be arguments over usage in internal code is why
we went with an "only if it is a clear problem, especially when the
function has important side effects" policy to avoid this. There are not a
too many examples in core, more in userland.
> I have to stop you here. It is absolutely NOT safe and reasonable to
ignore the output of array_pop. You popped it for a reason. If you just
care about removing the last element, then you should use unset. Unset
gives the intention. If I review code with array_pop, I’ll spend quite a
bit of time checking that it was intentionally ignoring the return value
(and leave a comment of the same, asking to use unset instead).
There are plenty of codebases where array_pop is used for this reason.
Identifying the last element of a php array.
unset($foo[array_key_last($foo)]); is only possible since 7.3 and not
widely used. array_pop is also shorter and faster for the same effect (when
used on unknown array shapes ofc). The examples are plenty
https://github.com/search?q=repo%3AWordPress%2FWordPress%20array_pop&type=code
Regards,
Volker