rfc:variadic_empty

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

rfc:variadic_empty [2015/02/21 04:47]
tpunt
rfc:variadic_empty [2017/09/22 13:28]
Line 1: Line 1:
-====== PHP RFC: Make empty() a Variadic ====== 
-  * Version: 0.1 
-  * Date: 2015-02-20 
-  * Author: Thomas Punt, tpunt@hotmail.co.uk 
-  * Status: Draft 
-  * First Published at: http://​wiki.php.net/​rfc/​variadic_empty 
  
-===== Introduction ===== 
-This RFC aims to enable ''​empty()''​ to accept any number of arguments. 
- 
-<code php> 
-// example current usage #1: 
-if (empty($a) || empty($b) || empty($c)) {} 
- 
-// example current usage #2: 
-if (!empty($a) && !empty($b) && !empty($c)) {} 
- 
-// new proposed usage #1: 
-if (empty($a, $b, $c)) {} 
- 
-// new proposed usage #2: 
-if (!empty($a, $b, $c)) {} 
-</​code>​ 
- 
-===== Proposal ===== 
-The proposal is to change ''​empty()''​ so that it has a variable arity. This will enable developers to write more compact code when checking for the emptiness of multiple expressions. 
- 
-As the above snippet demonstrates,​ the semantics of a variadic ''​empty()''​ should be the equivalent to logically OR'ing together multiple ''​empty()''​ invocations. Thus, if //any// arguments passed into ''​empty()''​ are considered falsy, then **true** will be returned - otherwise, **false** is returned. This behaviour seems to be the most prevalent usage of multiple empty checks in a condition, therefore benefitting the most end users. 
- 
-===== Justification ===== 
- 
-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: 
- 
-  * WordPress (( https://​github.com/​WordPress/​WordPress/​blob/​master/​wp-admin/​includes/​template.php#​L1963 )) 
-<code php> 
-if ( empty( $pointer_id ) || empty( $selector ) || empty( $args ) || empty( $args['​content'​] ) ) 
-</​code>​ 
-  * OpenCart (( https://​github.com/​opencart/​opencart/​blob/​45fc863fa068d82b5280890e6466a198faa54bff/​upload/​admin/​controller/​openbay/​ebay_profile.php#​L128 )) 
-<code php> 
-if (empty($setting['​dispatch_times'​]) || empty($setting['​countries'​]) || empty($setting['​returns'​])){ 
-</​code>​ 
-  * phpBB (( https://​github.com/​phpbb/​phpbb/​blob/​040d451dcca9ae54d8f4b7bdd2f231033765a8f2/​phpBB/​phpbb/​notification/​method/​jabber.php#​L48 )) 
-<code php> 
-return !( 
- empty($this->​config['​jab_enable'​]) || 
- empty($this->​config['​jab_host'​]) || 
- empty($this->​config['​jab_username'​]) || 
- empty($this->​config['​jab_password'​]) || 
- !@extension_loaded('​xml'​) 
- ); 
-</​code>​ 
-  * And so on... 
- 
-So this seems like quite a common need for users, and one that cannot be emulated in user-land code because of ''​empty()'''​s behaviour of suppressing undefined variables. Also, it will make ''​empty()''​ more inline with the not-too-disimillar ''​isset()'',​ which is good for the [[http://​en.wikipedia.org/​wiki/​Principle_of_least_astonishment|Principle of Least Astonishment]] (mainly aimed at neophyte developers). 
- 
-===== Backward Incompatible Changes ===== 
-No BC breakages. 
- 
-===== Proposed PHP Version(s) ===== 
-PHP 7.0 
- 
-===== Unaffected PHP Functionality ===== 
-The current functionality of ''​empty()''​ will be completely preserved. 
- 
-===== Proposed Voting Choices ===== 
-State whether this project requires a 2/3 or 50%+1 majority (see [[voting]]) 
- 
-===== Patches and Tests ===== 
- 
-PR: https://​github.com/​php/​php-src/​pull/​1109 
rfc/variadic_empty.txt · Last modified: 2017/09/22 13:28 (external edit)