Hello Internals!
The following RFC aims to make empty() have a variable arity: https://wiki.php.net/rfc/variadic_empty. This is a simple feature that enables for a short-hand notation of checking multiple expressions for emptiness (which is a pretty common thing to do). I have avoided including the is_() functions into this RFC because my main priority is to get this particular feature into PHP 7.0 before feature freeze. Provided I have some free time over the next week, I'll write up another RFC + patch to cover the is_() functions. Thanks,Tom
Hey Tom,
Patch looks solid (basically the same as the isset logic with OR instead of AND). I think it's fairly sane to have this feature because it compliments isset functionality (although I dislike "empty" personally - consistency is nice)
No RFC would be complete without a complaint about naming. "empty_expressions" ... I'll give you an empty expression!
But overall +1 on functionality and patch.
Cheers!
Leigh.
Hello Internals!
The following RFC aims to make empty() have a variable arity: https://wiki.php.net/rfc/variadic_empty. This is a simple feature that enables for a short-hand notation of checking multiple expressions for emptiness (which is a pretty common thing to do). I have avoided including the is_() functions into this RFC because my main priority is to get this particular feature into PHP 7.0 before feature freeze. Provided I have some free time over the next week, I'll write up another RFC + patch to cover the is_() functions. Thanks,Tom
Hello Internals!
The following RFC aims to make empty() have a variable arity: https://wiki.php.net/rfc/variadic_empty. This is a simple feature that enables for a short-hand notation of checking multiple expressions for emptiness (which is a pretty common thing to do). I have avoided including the is_() functions into this RFC because my main priority is to get this particular feature into PHP 7.0 before feature freeze. Provided I have some free time over the next week, I'll write up another RFC + patch to cover the is_() functions.
From the RFC:
This behaviour seems to be the most prevalent usage of multiple empty checks in a condition, therefore benefitting the most end users.
Here I disagree.
I would have assumed from the start that empty() would only return true if all of the entries are empty, i.e. AND things together.
If we enable variable arity then it will join the group of function where no user sanely can remember if in_array/etc. take the needle/haystack first or second argument. I.e. whether it will OR or AND all arguments.
From the RFC:
Also, it will make empty() more inline with the not-too-disimillar isset(), which is good for the Principle of Least Astonishment (mainly aimed at neophyte developers).
From http://php.net/manual/en/function.isset.php : "If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered."
But this is an AND: "... if all ... are set":
"isset($a,$b)" behaves like "isset($a) && isset($b)"
IMHO this absolutely violates your POLA because as I said this is also how I would assume empty() with variable arguments would work: Only return true if all are true.
My personal opinion is that any attempt to change this is ill-fated because people will no be able to memoize the exact usage of it because it would mean different things to different people and would just add more confusion.
-1 on the proposed change.
- Markus
Quoted text · 4 lines
From the RFC:
Also, it will make empty() more inline with the not-too-disimillar isset(),
Here I disagree.
I would have assumed from the start that empty() would only return true if all of the entries are empty, i.e. AND things together.
The problem stems from the fact that 'empty' is a falsy statement as it returns true when something is not set. Combining two positive results in another positive statement. Combining two negative statements results in a positive statement.....except when it doesn't. You always have to think about what is the correct way to combine them.
From the RFC:
In PHP, it is not uncommon to see conditionals consisting of multiple empty() invocations. This is evident by simply browsing through some popular open source projects
This is subjective opinion, but at least two of those code examples are horrible. In particular the phpBB code shows the problem of using compound 'falsy' statements:
return !(
empty($this->config['jab_enable']) ||
empty($this->config['jab_host']) ||
empty($this->config['jab_username']) ||
empty($this->config['jab_password']) ||
!@extension_loaded('xml')
);
Seriously, a double-negative at the end of a five piece statement that combines falsy things? Making it easier to write bad (imo) code does not seem a good reason for a change.
cheers
Dan