rfc:in_operator

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
rfc:in_operator [2015/02/02 13:10] – very basic draft kelunikrfc:in_operator [2015/02/20 12:50] – Under Discussion kelunik
Line 1: Line 1:
-====== PHP RFC: Your Title Here ====== +====== PHP RFC: In Operator ====== 
-  * Version: 0.1 +  * Version: 0.3 
-  * Date: 2015-02-02 +  * Date: 2015-02-20 
-  * Author: Niklas Kellerme@kelunik.com +  * Authors: Niklas Keller <me@kelunik.com>, Bob Weinand <bobwei9@hotmail.com> 
-  * Status: Draft+  * Status: Under Discussion
   * 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. 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. The ''in'' operator makes these checks way more readable. Additionally, it also works for ''Traversable''.
  
 ===== 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 
 +</code>
  
-  $stdClass new StdClass+<code php> 
-  $stdClass->foo = "bar"; +function gen () { 
-  $contains = "foo" in $stdClass; // true +    yield "foo"; 
-  $contains = "bar" in $stdClass; // false+    yield "bar"; 
 +
 + 
 +$contains "bar" in gen()
 +</code> 
 + 
 +If the first parameter is an array , it checks for every element whether it's contained in the ''$haystack'' or not. This is an advantage compared to ''in_array''. It doesn't check the values in ''$needle'' recursively, so it's still possible to check if an array contains another array, but makes checking multiple scalars easier: 
 + 
 +<code php> 
 +$contains ["foo", "bar"] in ["foo", "baz", "bar"]// true 
 +$contains = ["foo", "bar", "baz"in ["foo", "bar"]; // false 
 +$contains = [["foo", "bar"]] in [["foo", "bar"], ["foo", "baz"]]; // true 
 +$contains = [["foo", "bar"]] in ["foo", "bar"]; // false 
 +</code> 
 + 
 +For strings, it behaves exactly like ''strpos($haystack, $needle) !== false'': 
 +<code php> 
 +$contains = "foo" in "foobar"; // true 
 +$contains = "php" in "foobar"; // false 
 +</code> 
 + 
 +Objects are not supported, because we already have ''isset'' here. 
 + 
 +==== Why strict? ==== 
 +It's strict because otherwise something like ''"foo" in [0]'' would be true. 
 + 
 +==== Precedence ==== 
 +It should have the same precedence as ''instanceof'', so it's possible to negate it: 
 + 
 +<code php> 
 +if (!$input in $validValues) { 
 +    // ... 
 +
 +</code>
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
-New reserved keyword ''in''.+New reserved keyword ''in''. This affects function, constants, class and method names.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 31: Line 67:
  
 ===== RFC Impact ===== ===== RFC Impact =====
-==== To SAPIs ==== 
-Describe the impact to CLI, Development web server, embedded PHP etc. 
- 
-==== To Existing Extensions ==== 
-Will existing extensions be affected? 
- 
-==== To Opcache ==== 
-It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP. 
- 
-Please explain how you have verified your RFC's compatibility with opcache. 
- 
 ==== New Constants ==== ==== New Constants ====
 A ''T_IN'' constant for use with ext/tokenizer has been added. A ''T_IN'' constant for use with ext/tokenizer has been added.
Line 47: Line 72:
 ===== Open Issues ===== ===== Open Issues =====
 Make sure there are no open issues when the vote starts! Make sure there are no open issues when the vote starts!
- 
-===== Unaffected PHP Functionality ===== 
-List existing areas/features of PHP that will not be changed by the RFC. 
- 
-This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise. 
  
 ===== Future Scope ===== ===== Future Scope =====
Line 57: Line 77:
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-Requires a 2/3 majority.+Requires a 2/3 majority, simple yes / no vote.
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-Links to any external patches and tests go here. +TBD
- +
-If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed. +
- +
-Make it clear if the patch is intended to be the final patch, or is just a prototype.+
  
 ===== Implementation ===== ===== Implementation =====
-After the project is implemented, this section should contain  +TBD
-  - 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 ===== +
-Links to external references, discussions or RFCs+
  
 ===== 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.
rfc/in_operator.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1