I know that PHP doesn't have native Enumerations. But I have become accustomed to them from the Java world. I would love to use enums as a way to give predefined values which IDEs' auto completion features could understand.
Constants do the trick, but there's the namespace collision problem and (or actually because) they're global. Arrays don't have the namespace problem, but they're too vague, they can be overwritten at runtime and IDEs rarely (never?) know how to autofill their keys.
Are there any solutions/workarounds you commonly use? Does anyone recall whether the PHP guys have had any thoughts or decisions around enums?
Ok, here is my attempt that is Strongly typed, and very natural to use and define.
Definition:
Switch over Enum
Pass Enum as parameter (Strongly typed)
Echo Enum as string
Get Enum by integer
Get Enum by Name
The Enum Class:
Addition
You can ofcourse also add comments for IDEs
The top answer above is fantastic. However, if you
extend
it in two different ways, then whichever extension is done first results in a call to the functions will create the cache. This cache will then be used by all subsequent calls, no matter whichever extension the calls are initiated by ...To solve this, replace the variable and first function with:
I used classes with constants:
I have taken to using the approach below as it gives me the ability to have type safety for function parameters, auto complete in NetBeans and good performance. The one thing I don't like too much is that you have to call
[extended class name]::enumerate();
after defining the class.I realize this is a very-very-very old thread but I had a thought about this and wanted to know what people thought.
Notes: I was playing around with this and realized that if I just modified the
__call()
function that you can get even closer to actualenums
. The__call()
function handles all unknown function calls. So let's say you wan to make threeenums
RED_LIGHT, YELLOW_LIGHT, and GREEN_LIGHT. You can do so now by just doing the following:Once defined all you have to do is to call them again to get the values:
and you should get 0, 1, and 2. Have fun! This is also now up on GitHub.
Update: I've made it so both the
__get()
and__set()
functions are now used. These allow you to not have to call a function unless you want to. Instead, now you can just say:For both the creation and getting of the values. Because the variables haven't been defined initially, the
__get()
function is called (because there isn't a value specified) which sees that the entry in the array hasn't been made. So it makes the entry, assigns it the last value given plus one(+1), increments the last value variable, and returns TRUE. If you set the value:Then the
__set()
function is called and the last value is then set to the new value plus one (+1). So now we have a fairly good way to do enums and they can be created on the fly.Pointed out solution works well. Clean and smooth.
However, if you want strongly typed enumerations, you can use this:
With an Enum class looking like:
This way, enum values are strongly typed and
TestEnum::$TEST1 === TestEnum::parse('TEST1') // true statement