rfc:instanceof_improvements
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:instanceof_improvements [2020/05/19 18:55] – created maxsem | rfc:instanceof_improvements [2021/03/09 09:51] – maxsem | ||
---|---|---|---|
Line 7: | Line 7: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | The elevator pitch for the RFC. The first paragraph of this section will be slightly larger | + | As currently implemented, |
+ | <code php> | ||
+ | var_dump(new MyClass instanceof MyClass); // true | ||
+ | </ | ||
+ | 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 a class name | ||
+ | </ | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | I'd like to propose to modify | + | Make '' |
<code php> | <code php> | ||
- | ' | + | var_dump(' |
- | ' | + | var_dump(' |
- | $logger instanceof " | + | $type = ' |
- | $foo instanceof | + | var_dump(' |
- | $foo instanceof $someObject:: | + | |
</ | </ | ||
+ | |||
+ | ==== Types to support ==== | ||
+ | This proposal covers only concrete scalar types '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | All attempts to check against these types would evaluate to '' | ||
+ | |||
+ | * '' | ||
+ | |||
+ | ==== Legacy type aliases ==== | ||
+ | There are several legacy types, supported only for typecasts: '' | ||
+ | |||
+ | ==== Constant expressions on the left hand side ==== | ||
+ | The current implementation has a shortcut where if there is a constant expression to the left of '' | ||
+ | * In Java, a variable is required on the left side of '' | ||
+ | * In C#, constant expressions to the left of '' | ||
+ | * None of these support type names as strings, so the latter use case has no direct analogs. | ||
+ | |||
+ | I can see two possibilities why such constructs might appear in code: | ||
+ | * An clueless developer trying to achieve with '' | ||
+ | * A code generator went astray and generates something dubious. | ||
+ | Considering this, I don't think that adding support for constant expressions on LHS would do our end users any good. I propose to continue shortcutting such cases to false and additionally let the developers know they' | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 24: | Line 53: | ||
===== 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