rfc:instanceof_improvements

Differences

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

Link to this comparison view

Next revisionBoth sides next revision
rfc:instanceof_improvements [2020/05/19 18:55] – created maxsemrfc:instanceof_improvements [2021/03/08 16:04] – ++ maxsem
Line 7: Line 7:
  
 ===== Introduction ===== ===== Introduction =====
-The elevator pitch for the RFCThe first paragraph of this section will be slightly larger to give it emphasisplease write good introduction.+As currently implemented, the `instanceof` operator is inconsistent. 
 +<code php> 
 +var_dump(new MyClass instanceof MyClass); // true 
 +</code> 
 +So far so good? What if we tried to use a scalar type? They can be specified as parameter types just like class names, right? 
 +<code php> 
 +$x = 123; 
 +var_dump($x instanceof int); // false, right hand side is always treated as class name 
 +</code>
  
 ===== Proposal ===== ===== Proposal =====
-I'd like to propose to modify ''instanceof'' to support non-object types, as well as expressions on the right:+Make ''instanceof'' support non-object types:
 <code php> <code php>
-'foo' instanceof string            // true +var_dump('foo' instanceof string)       // true 
-'foo' instanceof 'string'          // true +var_dump('foo' instanceof ('string'))   // true 
-$logger instanceof "{$this->loggerType}Logger" +$type = 'string'; 
-$foo instanceof SomeClass::class   // kinda silly - but why the heck not? +var_dump('fooinstanceof $type);       // true
-$foo instanceof $someObject::class // a less silly variation +
 </code> </code>
  
Line 24: Line 30:
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
-PHP 8.0.+PHP 8.1.
  
 ===== RFC Impact ===== ===== RFC Impact =====
rfc/instanceof_improvements.txt · Last modified: 2022/04/18 10:51 by ilutov