rfc:enum_allow_static_properties

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
Last revisionBoth sides next revision
rfc:enum_allow_static_properties [2021/05/18 00:42] tandrerfc:enum_allow_static_properties [2021/06/15 13:49] tandre
Line 3: Line 3:
   * Date: 2021-05-17   * Date: 2021-05-17
   * Author: Tyson Andre, <tandre@php.net>   * Author: Tyson Andre, <tandre@php.net>
-  * Status: Under Discussion+  * Status: Voting
   * Implementation: https://github.com/php/php-src/pull/6997   * Implementation: https://github.com/php/php-src/pull/6997
   * First Published at: http://wiki.php.net/rfc/enum_allow_static_properties   * First Published at: http://wiki.php.net/rfc/enum_allow_static_properties
Line 27: Line 27:
   - [[https://en.wikipedia.org/wiki/Memoization|Memoization]] of expensive operations with no side effects (reading and parsing a large file from disk, cpu-intensive operations, service/db calls)   - [[https://en.wikipedia.org/wiki/Memoization|Memoization]] of expensive operations with no side effects (reading and parsing a large file from disk, cpu-intensive operations, service/db calls)
   - 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://en.wikipedia.org/wiki/Memoization|Memoization]], it may be useful to keep the result of expensive operations with no side effects in a static properties in the same module. Typically, php static properties are used where many other languages may have module functions and state in a file separate from a class definition (e.g. due to autoloading and coding standards requiring one class per file).
 +
 +<code php>
 +// https://en.wikipedia.org/wiki/Sprite_(computer_graphics)
 +enum Sprite {
 +    case FROG;
 +    case LOG;
 +    case GRASS;
 +    case WATER;
 +    // etc.
 +    
 +    /** @var array<string, MyModule\ImageData> */
 +    private static array $cache = [];
 +    
 +    public static function getImageData(SpriteArt $value): MyModule\ImageData {
 +        $key = $value->name;
 +        if (!isset(self::$cache[$key])) {
 +            self::$cache[$key] = self::loadImageData($value);
 +        }
 +        return self::$cache[$key];
 +    }
 +    
 +    // Called when is no longer used, the color scheme changed, etc.
 +    public static function clearImageData(SpriteArt $value): void {
 +        self::$cache = [];
 +    }
 +    
 +    // Slow operation: Read image data from disk and decode image data.
 +    public static function loadImageData(): void {
 +        // ...
 +    }
 +}
 +</code>
  
 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 146: 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 206: Line 242:
  
  
-===== Proposed Voting Choices ===== +===== Vote ===== 
-Yes/No, requiring a 2/3 majority+This is a Yes/No vote, requiring a 2/3 majority
 + 
 +Voting started on June 1, 2021 and ends on June 15, 2021
  
 +<doodle title="Allow static properties in enums" auth="tandre" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
 ===== References ===== ===== References =====
 https://externals.io/message/112626#113037 brought up the same suggestion. https://externals.io/message/112626#113037 brought up the same suggestion.
  
 [[enumerations|enums RFC]] [[enumerations|enums RFC]]
rfc/enum_allow_static_properties.txt · Last modified: 2021/06/16 03:29 by tandre