rfc:empty_isset_exprs

This is an old revision of the document!


RFC: Allow arbitrary expression arguments to empty() and isset()

Summary

This RFC proposes to allow arbitrary expressions as arguments to empty() and isset().

What is the current behavior?

Currently empty() and isset() only accept variables as arguments. Thus it is possible to write empty($foo), but it is not possible to write empty(foo()).

Trying to do so results in this not particularly helpful error message: “Can't use function return value in write context”.

For other expressions (not variables and not function calls) a parse error is thrown.

Why was this behavior chosen in the past?

Using empty() and isset() on a non-variable was disallowed previously, as the main purpose of these language constructs is to suppress an error message in case the passed variable does not exist.

As for function calls (and other expressions) it is already known that the value exists, using empty()/isset() is not necessary and !func()/func() !== null can be used instead.

Why should we change the behavior?

Even though !func() and empty(func()) would behave exactly the same, the latter is more readable in certain contexts. For example if func() is expected to return an array, it feels more natural to verify it's emptiness using empty() instead of !.

Furthermore the current behavior often is unexpected to newbies. empty() and isset() look like functions, so programmers new to PHP expect them to accept any value (in particular function call results).

Patch

The patch is available as a PR on Github: https://github.com/php/php-src/pull/54

Vote

Which of the language constructs should accept arbitrary arguments?
Real name Both empty() and isset() Only empty() None
ab (ab)   
brianlmoon (brianlmoon)   
cataphract (cataphract)   
colder (colder)   
dragoonis (dragoonis)   
drak (drak)   
hholzgra (hholzgra)   
hradtke (hradtke)   
ircmaxell (ircmaxell)   
kassner (kassner)   
lstrojny (lstrojny)   
lynch (lynch)   
mfonda (mfonda)   
mj (mj)   
nikic (nikic)   
pajoye (pajoye)   
patrickallaert (patrickallaert)   
rdohms (rdohms)   
salathe (salathe)   
stas (stas)   
weierophinney (weierophinney)   
Count: 3 14 4

The previous vote is obsolete and is left here only for reference:

Should empty() and isset() accept arbitrary arguments?
Real name yes no
cataphract (cataphract)  
colder (colder)  
ircmaxell (ircmaxell)  
kassner (kassner)  
kriscraig (kriscraig)  
laruence (laruence)  
mike (mike)  
mj (mj)  
neufeind (neufeind)  
nikic (nikic)  
patrickallaert (patrickallaert)  
salathe (salathe)  
stas (stas)  
tyrael (tyrael)  
Final result: 12 2
This poll has been closed.
rfc/empty_isset_exprs.1335790159.txt.gz · Last modified: 2017/09/22 13:28 (external edit)