Requests for comment/Typesafe enums

This RFC proposes a mechanism for creating typesafe enums. An implementation with tests and examples is provided.

Problem statement
By using enums, you can improve code in several ways. Many languages have native enum constructs, but PHP doesn't. In MediaWiki, class constants are usually used instead. However, PHP developers often roll enum implementations that are as good or nearly as good as native constructs in other languages.

Adding an enum mechanism to core would provide MediaWiki code the benefits of fully featured enums, including type safety, information hiding, meaningful conversion to strings, helper functions on sets of enum values, and extensibility.

Options
Options include:
 * Do nothing. There's nothing inherently wrong with class constants.
 * Use PHP's experimental SplEnum class. Disadvantages: this class is unintuitive to use, since enum consumers must instantiate enum values. Also, it's needlessly crufty—enum definitions still assign integers to each enum!
 * Build something better ourselves. Hopefully the proposed implementation, described below, fits the bill.

Current functionality
With the proposed implementation, you can define an enum like this:

The call to  can go in the same .php file as the enum definition. It assigns unique values to the static properties and sets up an index for helper functions. It must be performed before the enum is used.

Once you've defined your enum and called, here's what you can do:

There's also an interface,, which you can extend. For example: