rfc:treat_enum_instances_as_values

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:treat_enum_instances_as_values [2023/04/28 21:49] – Removed unused sections suitespacerndrfc:treat_enum_instances_as_values [2023/04/29 11:04] (current) – typo correction suggested by internals suitespacernd
Line 11: Line 11:
 This RFC proposes to allow the use of enum instances as array keys in PHP. More specifically, where errors are produces in any context where an enum instance is used without ->value or ->name, the engine should instead assume ->value. This RFC proposes to allow the use of enum instances as array keys in PHP. More specifically, where errors are produces in any context where an enum instance is used without ->value or ->name, the engine should instead assume ->value.
  
-This change aims to enhance the usability and consistency of enums in PHP by treating enum instances as symbols of their respective values - especially in the context of array keys. This will allow large amounts of existing code in various C-style languages to be compatible with PHP usage without affecting current projects. Further, this enables potential future RFC to enable array key type constraints in line with present == vs === semantics and ArrayAccess, without drastic engine overhauls.+This change aims to enhance the usability and consistency of enums in PHP by treating enum instances as symbols of their respective values - especially in the context of array keys. This will allow large amounts of existing code in various C-style languages to be compatible with PHP usage without affecting current projects.  
 + 
 +Further, this proposal provides potential future RFC a path to enable array key type constraints in line with present == vs === semantics and ArrayAccess, without drastic engine overhauls.
  
  
Line 39: Line 41:
    $palette = [    $palette = [
        1 => 'Red',        1 => 'Red',
-      2 => 'Green',+       2 => 'Green',
        3 => 'Blue',        3 => 'Blue',
    ];    ];
Line 111: Line 113:
        
       public function offsetGet(Month $which){       public function offsetGet(Month $which){
 +         //...
 +      }
 +   
 +   //...
 +   }
 +   class YourType implements ArrayAccess
 +   //...
 +   
 +      public function offsetGet(mixed $which){
          //...          //...
       }       }
Line 120: Line 131:
  
    $myobj = new MyType();    $myobj = new MyType();
 +   $yourobj = new YourType();
    $x = $myobj[Color::Yellow];   // error    $x = $myobj[Color::Yellow];   // error
 +   $y = $yourobj[Color::Yellow]; // fine
  
 Such a roadmap finally enables users to user to simply use match($this) and other userland syntactic sugar to simulate object keys. This gap can be further reduced and optimized from there. Such a roadmap finally enables users to user to simply use match($this) and other userland syntactic sugar to simulate object keys. This gap can be further reduced and optimized from there.
Line 132: Line 145:
  
 ===== Implementation ===== ===== Implementation =====
-Author's org has committed work on this issue and have it implemented, assuming community is in favor and this is not trivial for an existing member. Any guidance from those who have worked previously with enums is appreciated.+Author's org has committed work on this issue and having it implemented, assuming community is in favor and this is not trivial for an existing member. Any guidance from those who have worked previously with enums is appreciated.
  
  
rfc/treat_enum_instances_as_values.1682718549.txt.gz · Last modified: 2023/04/28 21:49 by suitespacernd