Phabstractic: Enumeration Data Type

I am proud to announce the addition of an enumeration data type to the asherwunk/phabstractic project.

Enumeration Data Type

An enumeration data type can be summed up by Wikipedia:

In computer programming, an enumerated type (also called enumeration or enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elementsmembers or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an enumerated type has values that are different from each other, and that can be compared and assigned, but which are not specified by the programmer as having any particular concrete representation in the computer’s memory; compilers and interpreters can represent them arbitrarily.

PHP doesn’t have an enumeration data type, so I built an enumeration generator of my own.  It actually won an award on!  It has been refactored to use the Configuration feature, formatted to follow the PSR standards as best it can, and relicensed under the MIT License.

Version 3 has made some changes.  Constants can not have their own custom values now, reducing complexity and error prone-ness.  This means that constants are not defined as an associative array! There has also been some changes to the internal enumerations, that being of checking the value against the constants before being set.

Enum Code Generation

The core of the enumeration class is code generated by the private ::createEnum(…) method.  This code uses eval() to execute some definition code to create a class that defines an enumeration.  Of course, in PHP this is really implemented as EnumerationClass::EnumerationValue.  The values assigned to the enumerated values of the enumeration are arbitrary, and generated by the code generator.  The main code generation (baker) in the class lies in ::createEnum(…) method:

In one fell swoop you can create an enumeration by defining the following:

It is actually possible to use a static method to create an enumeration as well:

Notice the inclusion of the following code in ::createEnum(…):


This ensures that no enumerator is created twice in the same namespace.

An enumeration not only acts as an enumerator data type, but can also be instantiated as an enumerated value itself. This is a little clunky, but it seemed the only way to organize it. If you are using the an actual enumerator class instantiation then you can compare the values like so:

(string) $instance == EnumClass::EnumValue;

I know that that can get really bizarre if not carefully watched, but that’s the only way I could think of organizing it. You are safer to set a value TO an enumeration:

$value = EnumClass::EnumValue;

But technically, that’s not an enumeration type.

View on GitHub

This is part of the Phabstractic Library.

If you appreciate my programming please help support me through my Patreon.

photo credit: six4x4 via photopin (license)


I'm just a wunk, trying to enjoy life. I am a cofounder of http// and I like computers, code, creativity, and friends.

You may also like...

Leave a Reply

%d bloggers like this: