rfc:in_operator

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:in_operator [2015/02/02 13:10] – very basic draft kelunikrfc:in_operator [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== PHP RFC: Your Title Here ====== +====== PHP RFC: In Operator ====== 
-  * Version: 0.1 +  * Version: 0.5.1 
-  * Date: 2015-02-02 +  * Date: 2015-03-15 
-  * Author: Niklas Kellerme@kelunik.com +  * Authors: Niklas Keller <me@kelunik.com>, Bob Weinand <bobwei9@hotmail.com> 
-  * Status: Draft+  * Status: Declined
   * First Published at: http://wiki.php.net/rfc/in_operator   * First Published at: http://wiki.php.net/rfc/in_operator
  
 ===== Introduction ===== ===== Introduction =====
-This RFC adds a new ''in'' operator. It simplifies ''contains'' checks for strings and arrays and checks for accessible properties in objects.+This RFC adds a new ''in'' operator which simplifies ''contains'' checks for strings and arrays. The ''in'' operator makes these checks way more readable and lowers the cognitive load. Additionally, it also works for ''Traversable''
 + 
 +===== Motivation ===== 
 +Checking if a specific input in an allowed range of value is a very common check in web application, therefore this operator simplifies those checks (and besides makes them a little bit faster). Currently, we have to use ''in_array($needle, $haystack, true)'' or ''strpos($haystack, $needle) !== false''. These functions have a inconsistent parameter order, so it's hard to remember which is the right one for each. Additionally, omitting the third parameter for ''in_array'' is very common which led to security vulnerabilities in the past.
  
 ===== Proposal ===== ===== Proposal =====
-Add a new operator ''(expr1) in (expr2)''For strings and arrays, it checks whether ''expr2'' contains ''expr1''.+Add a new operator ''(expr1) in (expr2)''It checks whether ''expr2'' contains ''expr1''.
  
-  $contains "foo" in ["a", "b", "c"]; // false +It uses strict comparison (''==='') for array values instances of ''Traversable'' and doesn't search recursively.
-  $contains "foo" in ["foo", "bar"]; // true +
-  $contains "foo" in "foobar"; // true +
-  $contains = "php" in "foobar"; /false+
  
-For objects, it works bit differentlyit checks if ''expr1'' is a accessible property name of the ''expr2''.+<code php> 
 +$contains = "foo" in ["a""b", "c"]; // false 
 +$contains = "foo" in ["foo", "bar"]; // true 
 +$contains = "foo" in [["foo"], ["bar"]]; // false 
 +$contains = "0e0" in ["0"]; // false, because of strict comparison 
 +$contains = 0 in ["0"]; // false, because of strict comparison
  
-  $stdClass new StdClass; +$contains ["foo"] in [["foo"], ["bar"]]; // true 
-  $stdClass->foo "bar"; +$contains = ["foo"in ["foo"]; // false 
-  $contains = "foo" in $stdClass; // true +</code>
-  $contains = "bar" in $stdClass; // false+
  
-===== Backward Incompatible Changes ===== +''Traversable''s are only iterated until there's a match.
-New reserved keyword ''in''.+
  
-===== Proposed PHP Version(s===== +<code php> 
-Next major release, at the time of writing PHP 7.+function gen () { 
 +    yield "foo"; 
 +    yield "bar"; 
 +    // code below here wouldn't be executed if "bar" matches 
 +    // because it stops if there's a match. 
 +}
  
-===== RFC Impact ===== +$contains "bar" in gen(); // true 
-==== To SAPIs ==== +$contains "baz" in gen(); // false 
-Describe the impact to CLI, Development web server, embedded PHP etc.+</code>
  
-==== To Existing Extensions ==== +If ''$haystack'' is a string, it's a simple ''contains'' check.
-Will existing extensions be affected?+
  
-==== To Opcache ==== +<code php> 
-It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP.+$contains "foo" in "foobar"; // true 
 +$contains "php" in "foobar"; // false 
 +</code>
  
-Please explain how you have verified your RFC's compatibility with opcache.+Other expressions than ''mixed in array|Traversable'' or ''string in string'' throw an ''EngineException''.
  
-==== New Constants ==== +==== Why strict? ==== 
-''T_IN'' constant for use with ext/tokenizer has been added.+It's strict because otherwise something like ''"foo" in [0]'' would pass.
  
-===== Open Issues ===== +==== Precedence ==== 
-Make sure there are no open issues when the vote starts!+It should have the same precedence as ''instanceof'', so it's possible to negate it:
  
-===== Unaffected PHP Functionality ===== +<code php> 
-List existing areas/features of PHP that will not be changed by the RFC.+if (!$input in $validValues) { 
 +    // ... 
 +
 +</code>
  
-This helps avoid any ambiguityshows that you have thought deeply about the RFC's impact, and helps reduces mail list noise.+===== Backward Incompatible Changes ===== 
 +New reserved keyword ''in''This affects functionconstant and class**but not** class constant and method names, because it depends on the context sensitive lexer being merged.
  
-===== Future Scope ===== +===== Proposed PHP Version(s) ===== 
-None.+Next major release, at the time of writing PHP 7.
  
-===== Proposed Voting Choices ===== +===== RFC Impact ===== 
-Requires a 2/3 majority.+==== New Constants ==== 
 +A ''T_IN'' constant for use with ext/tokenizer has been added.
  
-===== Patches and Tests ===== +===== Future Scope ===== 
-Links to any external patches and tests go here.+There could be a syntax that allows to check for multiple values at once, e.g. 
 +<code php> 
 +$contains = ...["foo", "bar"] in ["foo", "baz", "bar"]; 
 +</code>
  
-If there is no patchmake it clear who will create a patch, or whether a volunteer to help with implementation is needed.+===== Votes ===== 
 +Requires a 2/3 majority. Even if it passes, it will //only// get merged if the [[rfc/context_sensitive_lexer|context sensitive lexer]] gets merged.
  
-Make it clear if the patch is intended to be the final patch, or is just a prototype.+<doodle title="Introduce the in operator?" auth="kelunik" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
-===== Implementation ===== +Voting started on 2015-03-15 and ends on 2015-03-29.
-After the project is implemented, this section should contain  +
-  the version(s) it was merged to +
-  a link to the git commit(s) +
-  a link to the PHP manual entry for the feature+
  
-===== References ===== +===== Patches and Tests ===== 
-Links to external references, discussions or RFCs+  * https://github.com/php/php-src/pull/1121
  
 ===== Rejected Features ===== ===== Rejected Features =====
 Keep this updated with features that were discussed on the mail lists. Keep this updated with features that were discussed on the mail lists.
 +
 +===== Changelog =====
 +  * v0.5: Removed integer support, so the strictness is consistent.
 +  * v0.4: Removed possibility to check multiple values using an array.
rfc/in_operator.1422882658.txt.gz · Last modified: 2017/09/22 13:28 (external edit)