rfc:treat_enum_instances_as_values
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:treat_enum_instances_as_values [2023/04/28 21:42] – created suitespacernd | rfc:treat_enum_instances_as_values [2023/04/29 07:02] – typo suitespacernd | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Your Title Here ====== | + | ====== PHP RFC: Treat Enum Instances as Values |
* Version: 0.1 | * Version: 0.1 | ||
* Date: 2023-04-28 | * Date: 2023-04-28 | ||
Line 11: | Line 11: | ||
This RFC proposes to allow the use of enum instances as array keys in PHP. More specifically, | This RFC proposes to allow the use of enum instances as array keys in PHP. More specifically, | ||
- | 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 | + | 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 | ||
Line 25: | Line 27: | ||
Consider the following enum and array: | Consider the following enum and array: | ||
- | enum Color: int { | + | enum Color: int { |
- | case red = 1; | + | |
- | case green = 2; | + | |
- | case blue = 3; | + | |
- | } | + | |
- | enum WeekDays: int { | + | enum WeekDays: int { |
- | case monday = 1; | + | |
- | case tuesday = 2; | + | |
- | case wednesday = 3; | + | |
- | } | + | |
- | $palette = [ | + | $palette = [ |
- | 1 => ' | + | |
- | 2 => ' | + | |
- | 3 => ' | + | |
- | ]; | + | |
Currently, the following code produces an error: | Currently, the following code produces an error: | ||
- | Color $red = Color:: | + | Color $red = Color:: |
- | $paint = $palette[$red]; | + | |
With this proposal, the code above should not produce an error and should work as expected: | With this proposal, the code above should not produce an error and should work as expected: | ||
- | Color $red = Color:: | + | Color $red = Color:: |
- | $paint = $palette[$red]; | + | |
Last on a more opinionated note; the author suggests that enum cases are userland symbols and should imply value. Just as there is no need in the following scenario: | Last on a more opinionated note; the author suggests that enum cases are userland symbols and should imply value. Just as there is no need in the following scenario: | ||
- | $a = 1; | + | $a = 1; |
- | $b = ' | + | |
- | $list = [ | + | $list = [ |
- | $a => 'some value', | + | |
- | $b => $some_value; | + | |
- | ]; | + | |
vs | vs | ||
- | $list = [ | + | $list = [ |
- | $a-> | + | |
- | $b-> | + | |
- | ]; | + | |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 90: | Line 92: | ||
Could be a toggle if community desired | Could be a toggle if community desired | ||
- | ===== Open Issues ===== | ||
- | Make sure there are no open issues when the vote starts! | ||
- | |||
- | ===== Unaffected PHP Functionality ===== | ||
- | List existing areas/ | ||
- | |||
- | 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 108: | Line 103: | ||
Future RFCs are recommended to enable | Future RFCs are recommended to enable | ||
- | Day::Monday == Color:: | + | Day::Monday == Color:: |
- | Day::Monday === Color::Red //false | + | |
Which could in turn allow | Which could in turn allow | ||
- | class MyType implements ArrayAccess{ | + | |
+ | class MyType implements ArrayAccess | ||
//... | //... | ||
- | public function offsetGet(Month $which){ | + | |
- | //... | + | //... |
+ | | ||
+ | |||
+ | //... | ||
} | } | ||
+ | class YourType implements ArrayAccess | ||
+ | //... | ||
+ | |||
+ | public function offsetGet($mixed $which){ | ||
+ | //... | ||
+ | } | ||
//... | //... | ||
- | } | + | } |
Making the desirable | Making the desirable | ||
- | $myobj = new MyType(); | + | $myobj = new MyType(); |
- | $x = $myobj[Color:: | + | $yourobj = new YourType(); |
+ | $x = $myobj[Color:: | ||
+ | $y = $yourobj[Color:: | ||
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 139: | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | Include these so readers know where you are heading and can discuss the proposed voting options. | + | A two-thirds majority is required for this proposal to be accepted. |
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
Line 138: | Line 145: | ||
===== Implementation ===== | ===== Implementation ===== | ||
- | Author' | + | Author' |
===== References ===== | ===== References ===== | ||
- | A robust discussion about further pros/ | + | A robust discussion about further pros/ |
- | Mailing list introduction and opening RFC- https:// | + | https:// |
+ | |||
+ | Mailing list introduction and opening RFC- | ||
+ | https:// | ||
Mailing list post-RFC discussion: TBD | Mailing list post-RFC discussion: TBD | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
None to date | None to date |
rfc/treat_enum_instances_as_values.txt · Last modified: 2023/04/29 11:04 by suitespacernd