Skip to content

RFC: Allow static properties in enums #6997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

TysonAndre
Copy link
Contributor

@TysonAndre TysonAndre commented May 17, 2021

These were forbiddden in the initial RFC, which was already very large/complex.
Others had suggested allowing static properties,
e.g. https://externals.io/message/112626#113037

Continue to forbid instance properties.

There are various use cases where static properties would be useful:

  1. Using traits that declare static properties without getting a fatal error.
  2. Static methods related to an enum that that act on immutable enum instances
    (e.g. EnvironmentEnum::current() could return ::PROD, ::STAGE, ::DEV)

The alternatives to static properties would have drawbacks,
such as shared state being more visible than intended (not private),
or not being able to fully use typed properties.

RFC: https://wiki.php.net/rfc/enum_allow_static_properties

@TysonAndre TysonAndre requested a review from iluuu1994 May 17, 2021 13:35
@krakjoe krakjoe added the RFC label May 17, 2021
@TysonAndre TysonAndre changed the title Proposal: Allow static properties in enums RFC: Allow static properties in enums May 17, 2021
These were forbiddden in the initial RFC, which was already very large/complex.
Others had suggested allowing static properties,
e.g. https://externals.io/message/112626#113037

Continue to forbid instance properties.

There are various use cases where static properties would be useful:

1. Using traits that declare static properties without getting a fatal error.
2. Static methods related to an enum that that act on immutable enum instances
   (e.g. `EnvironmentEnum::current()` could return `::PROD`, `::STAGE`, `::DEV`)

The alternatives to static properties would have drawbacks,
such as shared state being more visible than intended (not `private`),
or not being able to use typed properties.
@TysonAndre TysonAndre force-pushed the enum-static-property branch from 237a532 to 0e2733b Compare May 17, 2021 14:04
@iluuu1994
Copy link
Member

I'm not super convinced by the use case. Using static variables in enums would certainly not be great. Methods will behave differently in different environments even if the case is the same. This is certainly less than optimal. On the other hand, I do think that needlessly restricting the language to babysit developers is not good unless there's a technical reason to do so, which doesn't seem to be the case here.

@mvorisek
Copy link
Contributor

mvorisek commented Jun 2, 2021

RFC seems like it will not pass. Static variables can be emulated with a static method with a static variable.

@TysonAndre
Copy link
Contributor Author

RFC seems like it will not pass.

Agreed.

Static variables can be emulated with a static method with a static variable.

The fact that you can do this was mentioned in the RFC https://wiki.php.net/rfc/enum_allow_static_properties#this_is_better_than_alternative_ways_that_can_be_used_to_store_shared_state

Compared to alternatives such as global variables and local static variables or static properties of placeholder classes, this is a useful option to have for the following reasons:

  1. Property types can be used and enforced at runtime (and checked by type checkers).
  2. Visibility is easier to enforce and read (compared to global variables or static properties declared in other classes).
  3. It is easier to reset static properties in unit tests (compared to static variables).
  4. This can result in more concise and easier to understand code.

@TysonAndre TysonAndre closed this Jun 2, 2021
@TysonAndre TysonAndre deleted the enum-static-property branch June 2, 2021 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants