rfc:enum_v2

This is an old revision of the document!


PHP RFC: Enumeration type, version 2

Introduction

The elevator pitch for the RFC. The first paragraph of this section will be slightly larger to give it emphasis; please write a good introduction.

Proposal

A simple enum:

enum Letters {
    a,          // Enum constants start with 0 by default
    b,          // 1, always previous value +1 if not specified explicitly
    c = 10,     // Values can be set explicitly
    d,          // 11
    e = 1,      // Constants with duplicate values are allowed
    f = 2 * 2,  // Can use the same expressions as class constants
    g = f,      // Can use other constants too
    h = g + 10, // And in expressions too
    H,          // Valid, constant names are case sensitive
    i,          // Optional comma after the last constant is permitted
}

A binary enum:

binary enum FileMode {
    Read = 1,
    Write = 2,
    Execute = 1 << 2,
    ReadWrite = Read | Write,
}

Enums are classes

Internally, enums are classes and enum constants are public class constants. This makes them the fourth OOP-ey type in PHP, along with `class`, `interface` and `trait`. They can be autoloaded just like the former types. All enums inherit from this base class (here is PHP pseudocode):

final // In the sense that userspace can't inherit from it
class Enum {
    private int $value;
    private function __construct(); // It shouldn't be possible to create enums like this: $foo = new Enum();
    public function isBinary() : bool;
    public function __toString(): string {
        return (string)(int)$this->value;
    }
    // Whether the current value is represented by one of this enum's constants
    // or their combination for binary enums
    public function isKnownValue() : bool;
}

Backwards Incompatible Changes

`enum` and `binary` will become reserved keywords. Class name `Enum` will become unavailable.

Proposed PHP Version(s)

Next PHP 8.x (8.1?)

Open Issues

Make sure there are no open issues when the vote starts!

Unaffected PHP Functionality

List existing areas/features of PHP that will not be changed by the RFC.

This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise.

Future Scope

This section details areas where the feature might be improved in future, but that are not currently proposed in this RFC.

Proposed Voting Choices

* Accept this RFC (yes / no)? * What should be Enum fully qualified name (\Enum / \PHP\Enum)?

Patches and Tests

Links to any external patches and tests go here.

If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed.

Make it clear if the patch is intended to be the final patch, or is just a prototype.

For changes affecting the core language, you should also provide a patch for the language specification.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

Links to external references, discussions or RFCs * https://wiki.php.net/rfc/enum

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/enum_v2.1589467523.txt.gz · Last modified: 2020/05/14 14:45 by maxsem