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?
Well, for a simple java like enum in php, I use:
And to call it:
But I'm a PHP beginner, struggling with the syntax so this might not be the best way. I experimented some with Class Constants, using Reflection to get the constant name from it's value, might be neater.
Four years later I came across this again. My current approach is this as it allows for code completion in the IDE as well as type safety:
Base class:
Example Enum:
Example usage:
Note that all instances of the same enum entry are the same:
You can also use it inside of a switch statement:
You can also create an enum entry by name or value:
Or you can just get the name (i.e. the function name) from an existing enum entry:
Some good solutions on here!
Here's my version.
I think the main disadvantage is that enum members do have to be separately declared and instantiated, due to the descriptions and PHP's inability to construct objects at static member declaration time. I guess a way round this might be to use reflection with parsed doc comments instead.
The abstract enum looks like this:
Here's an example concrete enum:
Which can be used like this:
And produces this output:
I made a library based on Brian Cline's answer, it is named greg0ire/enum Enjoy!
Now you can use The SplEnum class to build it natively. As per the official documentation.
Please note, it's an extension which has to be installed, but not available by default. Which comes under Special Types described in the php website itself. The above example is taken from the PHP site.
Depending upon use case, I would normally use something simple like the following:
However, other use cases may require more validation of constants and values. Based on the comments below about reflection, and a few other notes, here's an expanded example which may better serve a much wider range of cases:
By creating a simple enum class that extends BasicEnum, you now have the ability to use methods thusly for simple input validation:
As a side note, any time I use reflection at least once on a static/const class where the data won't change (such as in an enum), I cache the results of those reflection calls, since using fresh reflection objects each time will eventually have a noticeable performance impact (Stored in an assocciative array for multiple enums).
Now that most people have finally upgraded to at least 5.3, and
SplEnum
is available, that is certainly a viable option as well--as long as you don't mind the traditionally unintuitive notion of having actual enum instantiations throughout your codebase. In the above example,BasicEnum
andDaysOfWeek
cannot be instantiated at all, nor should they be.