# Mark some functions with PREFER_REF

- Thread: https://externals.io/message/83612
- Original archive: https://news-web.php.net/php.internals/83612
- Started by Nikita Popov on 2015-02-23T21:36:08+00:00
- Messages: 3
- RFC: Reclassify E_STRICT notices (Implemented (PHP 7.0)) — https://externals.io/rfc/reclassify_e_strict.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #83612 — Nikita Popov — 2015-02-23T21:36:08+00:00

URL: https://externals.io/message/83612#83612

Hi internals!

With regard to my `E_STRICT` RFC [1] Yasuo and another off-the-record mail
expressed a concern about the "Only variables should be passed by
reference" strict standards warning.

This notices makes sense in a lot of places, e.g.

    array_push(getArray(), 1, 2, 3);

is totally pointless code if getArray() doesn't return a reference.
array_push will just modify a temporary array in this case.

However other usages like

    $top = array_pop(getArray());

are less clear: `array_pop()` both has a side-effect (removing the top
element of an array) and a meaningful return value (the top element of the
array). As PHP does not have a dedicated function for getting the
first/last element of an array, many people use code like this (which
currently throws E_STRICT) instead.

I'd like to mark a few such functions with PREFER_REF (i.e. allow passing
both references and values). In particular `array_pop()` and `array_shift()`.

Does anybody have a problem with us doing this? Are there other functions
which should use PREFER_REF?

The motivation behind this is to make sure we only throw this notice in
cases where the code is actually buggy/nonsensical instead of just making
people ignore it altogether.

Thanks,
Nikita

  [1]: https://wiki.php.net/rfc/reclassify_e_strict

---

## #83655 — Nikita Popov — 2015-02-24T11:37:47+00:00

URL: https://externals.io/message/83612#83655 · In reply to: #83612

> Hi internals!
>
> With regard to my `E_STRICT` RFC [1] Yasuo and another off-the-record mail
> expressed a concern about the "Only variables should be passed by
> reference" strict standards warning.
>
> This notices makes sense in a lot of places, e.g.
>
>     array_push(getArray(), 1, 2, 3);
>
> is totally pointless code if getArray() doesn't return a reference.
> array_push will just modify a temporary array in this case.
>
> However other usages like
>
>     $top = array_pop(getArray());
>
> are less clear: `array_pop()` both has a side-effect (removing the top
> element of an array) and a meaningful return value (the top element of the
> array). As PHP does not have a dedicated function for getting the
> first/last element of an array, many people use code like this (which
> currently throws E_STRICT) instead.
>
> I'd like to mark a few such functions with PREFER_REF (i.e. allow passing
> both references and values). In particular `array_pop()` and `array_shift()`.
>
> Does anybody have a problem with us doing this? Are there other functions
> which should use PREFER_REF?
>
> The motivation behind this is to make sure we only throw this notice in
> cases where the code is actually buggy/nonsensical instead of just making
> people ignore it altogether.
>

A few people have pointed out OTR that the same applies to `reset()` and
 `end()`. Both can be used to get the first/last element (and change the IAP
as a side effect). So I'd add PREFER_REF to those as well. (Interestingly
 `key()` and `current()` already use PREFR_REF even though they don't modify the
array at all, probably this is a performance optimization.)

Nikita

---

## #83663 — Dmitry Stogov — 2015-02-24T13:55:16+00:00

URL: https://externals.io/message/83612#83663 · In reply to: #83655

> On Mon, Feb 23, 2015 at 10:36 PM, Nikita Popov <nikita.ppv@gmail.com>
> wrote:
>
> > […]
> the
> > […]
>
> A few people have pointed out OTR that the same applies to `reset()` and
> `end()`. Both can be used to get the first/last element (and change the IAP
> as a side effect). So I'd add PREFER_REF to those as well. (Interestingly
> `key()` and `current()` already use PREFR_REF even though they don't modify the
> array at all, probably this is a performance optimization.)
>

 `key()` and `current()` shouldn't use it.
It was an optimization from PHP5 time that makes for PHP-7 more harm than
benefit.

According to `array_pop()`, `array_shift()`, `reset()` and `end()`, it's not a
problem to avoid this warning.
But note that such usage of these functions may be really inefficient,
because they may need to duplicate the passed array first.

Thanks. Dmitry.



>
> Nikita
