Interesting thread resurrection, so I shall contribute my two cents...
gir wrote:Does anybody know of a class that has implemented enumish behaviour?
We should define "enumish behaviour". One could define a general enumeration concept of a user defined type with a fixed number of possible values. This appears to be what kelphis was asking for in post #6.
An enumeration in C and C++ is a fixed list of named integral compile-time constants such that if no value is explicitly assigned to an enumerator, its value is one greater than the previous enumerator's value, and if the first enumerator is not explicitly assigned a value, its value is zero. An enumeration definition defines a type (but in C there is no safety provided at all against assigning a value to an object of an enum type such that the value is outside of the list of enumerator values). kelphis' examples in posts #1 and #6 appear to be of this kind.
Protato's suggestion seems more along the lines of a C and C++ enumeration, except that it is extensible at run time, is not restricted to integer values, and each enumeration is just an object rather than a type. We could make the addEnum() method's second parameter optional in order to emulate the lack of an explicit value assignment (and restrict the values to integers). On the other hand, creating an enumeration is more cumbersome than in C and C++, and accessing an enumerator looks very much like the method call that it is rather than the accessing of a named constant. Since the enumeration is not a type, one cannot create objects of a particular enumeration type, so the use case of this enum class seems to be restricted as a list of immutable name/value pairs.
I think that by using [man]array_flip/man, one could take advantage that keys in an array in PHP can be defined much like how enumerator values are defined in C and C++ enumerations, and thus allow for a less cumbersome way of creating an enumeration. Overloading __get would allow for a more desirable syntax.
NogDog's solution seems more along the lines of the more general enumeration concept. Through inheritance, one can create enumeration types. One drawback though is that each enumeration object will have its own array of values, despite this array being identical across all objects of its (sub)type. However, the PHP interpreter might be smart enough to recognise this and apply an appropriate optimisation (copy on write?), especially considering that the initialisation is performed as part of the variable declaration.