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
Quoted text · 11 lines
Hi internals!
With regard to my
E_STRICTRFC [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()andarray_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
Quoted text · 14 lines
On Mon, Feb 23, 2015 at 10:36 PM, Nikita Popov [email protected] wrote:
Hi internals!
With regard to my
E_STRICTRFC [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()andarray_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()andend(). 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. (Interestinglykey()andcurrent()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