Primus2: New Data Type (enum)


This code is obsolete, refers to an obsolete version, and has moved to another project.  This code and latest version are found in asherwunk/phabstractic.  There’s also a current blog post on this.

I am proud to announce the addition of an enumeration data type to the Primus Artificial Intelligence Framework: Falcraft Module

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 (the license of the Primus Artificial Intelligence Framework).

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
photo credit: This can mean only one thing… 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: