rfc:instanceof_improvements

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
rfc:instanceof_improvements [2021/03/08 17:40] maxsemrfc:instanceof_improvements [2021/03/09 09:51] maxsem
Line 7: Line 7:
  
 ===== Introduction ===== ===== Introduction =====
-As currently implemented, the `instanceofoperator is inconsistent.+As currently implemented, the ''instanceof'' operator is inconsistent.
 <code php> <code php>
 var_dump(new MyClass instanceof MyClass); // true var_dump(new MyClass instanceof MyClass); // true
Line 36: Line 36:
  
 ==== Legacy type aliases ==== ==== Legacy type aliases ====
 +There are several legacy types, supported only for typecasts: ''(integer)'', ''(double)'', ''(boolean)'' and ''(binary)''. They have never worked for any other situations: e.g. the first parameter of ''function f(integer $x)'' is interpreted as a class called ''integer'', we even started issuing warnings in such situations since 8.0. I propose to extend this kind of treatment to ''instanceof'' too, issuing the same warning.
  
 ==== Constant expressions on the left hand side ==== ==== Constant expressions on the left hand side ====
Line 43: Line 43:
   * In C#, constant expressions to the left of ''is'' produce a correct result but a compiler warning is issued.   * In C#, constant expressions to the left of ''is'' produce a correct result but a compiler warning is issued.
   * None of these support type names as strings, so the latter use case has no direct analogs.   * 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 ''123 instanceof $type'' something for which ''$type === "int"'' is a better solution.
 +  * 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're doing something wrong with ''E_COMPILE_WARNING''.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/instanceof_improvements.txt · Last modified: 2022/04/18 10:51 by ilutov