rfc:enum_allow_static_properties
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:enum_allow_static_properties [2021/05/18 00:20] – tandre | rfc:enum_allow_static_properties [2021/06/16 03:29] (current) – tandre | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Allow static properties in enums ====== | ====== PHP RFC: Allow static properties in enums ====== | ||
- | * Version: 0.1 | + | * Version: 0.2 |
* Date: 2021-05-17 | * Date: 2021-05-17 | ||
* Author: Tyson Andre, < | * Author: Tyson Andre, < | ||
- | * Status: | + | * Status: |
* Implementation: | * Implementation: | ||
* First Published at: http:// | * First Published at: http:// | ||
Line 27: | Line 27: | ||
- [[https:// | - [[https:// | ||
- Keeping track of which enum case reflects the current state of a state machine or system | - Keeping track of which enum case reflects the current state of a state machine or system | ||
+ | |||
+ | E.g. for [[https:// | ||
+ | |||
+ | <code php> | ||
+ | // https:// | ||
+ | enum Sprite { | ||
+ | case FROG; | ||
+ | case LOG; | ||
+ | case GRASS; | ||
+ | case WATER; | ||
+ | // etc. | ||
+ | | ||
+ | /** @var array< | ||
+ | private static array $cache = []; | ||
+ | | ||
+ | public static function getImageData(SpriteArt $value): MyModule\ImageData { | ||
+ | $key = $value-> | ||
+ | if (!isset(self:: | ||
+ | self:: | ||
+ | } | ||
+ | return self:: | ||
+ | } | ||
+ | | ||
+ | // Called when is no longer used, the color scheme changed, etc. | ||
+ | public static function clearImageData(SpriteArt $value): void { | ||
+ | self:: | ||
+ | } | ||
+ | | ||
+ | // Slow operation: Read image data from disk and decode image data. | ||
+ | public static function loadImageData(): | ||
+ | // ... | ||
+ | } | ||
+ | } | ||
+ | </ | ||
For example, one way to represent fetching the current environment (of a known enumeration of environments) would be a static method on the environment enum itself. The environment instance is immutable, but the environment being loaded depends on a file. | For example, one way to represent fetching the current environment (of a known enumeration of environments) would be a static method on the environment enum itself. The environment instance is immutable, but the environment being loaded depends on a file. | ||
Line 70: | Line 104: | ||
This is also useful because it allows enums to '' | This is also useful because it allows enums to '' | ||
+ | |||
+ | Projects may wish to enforce their own coding standards on how to appropriately use static properties in enums - e.g. I can imagine different projects may have different opinions, but having the functionality available to make use of would help their maintainers | ||
+ | |||
+ | * One project may forbid publicly visible static properties. | ||
+ | * Another project may allow uses of static properties, but only if the enum's methods would be idempotent or appear to the callers to be free of side effects. (e.g. to permit memoization) | ||
+ | * Another project may forbid static properties except when inherited from a trait that has other non-enum use cases. | ||
+ | |||
+ | Quoting [[http:// | ||
+ | |||
+ | > PHP is and should remain: | ||
+ | > 1) a pragmatic web-focused language | ||
+ | > 2) a loosely typed language | ||
+ | > 3) a language which caters to the skill-levels and platforms of a wide range of users | ||
==== This minimizes the backward compatibility impact of adding static properties to traits ==== | ==== This minimizes the backward compatibility impact of adding static properties to traits ==== | ||
Line 112: | Line 159: | ||
In some cases, enums may be associated with shared functionality that uses shared state, and static properties may be the most practical way for a developer/ | In some cases, enums may be associated with shared functionality that uses shared state, and static properties may be the most practical way for a developer/ | ||
+ | |||
+ | The following are potential use cases: | ||
- Migrating an application or library from other programming languages to a similar API in php, where files can have variables local to a module or if static variables are allowed. (For example, Java also allows static properties on enums whether or not they are final https:// | - Migrating an application or library from other programming languages to a similar API in php, where files can have variables local to a module or if static variables are allowed. (For example, Java also allows static properties on enums whether or not they are final https:// | ||
- | - Migrate | + | - Migrating |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 131: | Line 180: | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
Instance properties continue to be forbidden on enums. | Instance properties continue to be forbidden on enums. | ||
+ | |||
+ | PHP enums were already able to have instance and static methods. | ||
===== Discussion ===== | ===== Discussion ===== | ||
Line 166: | Line 217: | ||
Also a clarification, | Also a clarification, | ||
</ | </ | ||
+ | |||
+ | Counterarguments include: | ||
- [[# | - [[# | ||
Line 171: | Line 224: | ||
- An application may start off by only using pure static methods to an enum. But it may later unexpectedly need to add shared state to new or existing methods after that design was already finalized, and continuing to implement the functionality in static methods on the enum instance would be the most consistent with previous design decisions. | - An application may start off by only using pure static methods to an enum. But it may later unexpectedly need to add shared state to new or existing methods after that design was already finalized, and continuing to implement the functionality in static methods on the enum instance would be the most consistent with previous design decisions. | ||
- | ===== Proposed Voting Choices | + | ==== Would prefer that enums are collections of pure(side effect free) methods |
- | Yes/No, requiring a 2/3 majority | + | |
+ | https:// | ||
+ | |||
+ | < | ||
+ | Personally, I'd prefer to see enums as value objects only, adding static | ||
+ | properties allow to implementation of statically conditional behaviour. | ||
+ | IMO enums should consist only of pure functions. This is why I'd vote NO on | ||
+ | this proposal. | ||
+ | |||
+ | Cheers, | ||
+ | Michał Marcin Brzuchalski | ||
+ | </ | ||
+ | |||
+ | - Some operations that have no side effects and satisfying different definitions of pure (reading and parsing a large unchanging file from disk, cpu or memory-intensive operations, read-only service/db calls) may benefit from [[https:// | ||
+ | - Some end users may disagree with this philosophy, while others may be reluctantly forced into maintaining shared state due to unexpected feature requests or business logic reasons that weren' | ||
+ | |||
+ | |||
+ | ===== Vote ===== | ||
+ | This is a Yes/ | ||
+ | |||
+ | Voting started on June 1, 2021 and ends on June 15, 2021 | ||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== References ===== | ===== References ===== | ||
https:// | https:// | ||
[[enumerations|enums RFC]] | [[enumerations|enums RFC]] |
rfc/enum_allow_static_properties.txt · Last modified: 2021/06/16 03:29 by tandre