RFC: Allow arbitrary expression arguments to empty() and isset()
- Date: 2012-04-12
- Author: Nikita Popov email@example.com
- Status: Implemented
- Original discussion: http://markmail.org/thread/gnci76ugtwda2t42 (Apr 10, 2012 3:53 pm)
- RFC announcement: http://markmail.org/thread/ju2jyrenxkeddrnt (Apr 12, 2012 2:42 pm)
This RFC proposes to allow arbitrary expressions as arguments to
What is the current behavior?
isset() only accept variables as arguments. Thus it is possible to write
but it is not possible to write
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?
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
not necessary and
func() !== null can be used instead.
Why should we change the behavior?
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
Furthermore the current behavior often is unexpected to newbies.
isset() look like functions, so programmers
new to PHP expect them to accept any value (in particular function call results).
The patch is available as a PR on Github: https://github.com/php/php-src/pull/54
Change only empty()?
After further discussion it seems like it might be better to only add expression support for
empty(), but not for
original RFC included
isset(), because changing only one of the language constructs seemed inconsistent (as they are so similar).
On the other hand, using
isset() on function calls and other expressions doesn't seem particularly useful.
is semantically unclear (how can
someFunction() not be set?) and could be interpreted as a check whether the function itself exists.
isset() to accept expressions would probably only cause confusion.
The previous vote is obsolete and is left here only for reference:
The RFC was accepted with the option “Only empty()”. The relevant commit it https://github.com/php/php-src/commit/ec061a93c53c8cde10237741e98e992c1a05d148.