Download the PHP package xaamin/enum without Composer
On this page you can find all versions of the php package xaamin/enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package enum
PHP Enum implementation
This package offers enums in PHP. We don't use a simple "value" representation, so you're always working with the enum object. This allows for proper autocompletion and refactoring in IDEs.
Why?
Using an enum instead of class constants provides the following advantages:
- You can enrich the enum with alias methods names (e.g.
draft()
,published()
, …) - You can extend the enum to add new values (make your enum
final
to prevent it) - You can get a list of all the possible values (see below)
This Enum class is not intended to replace class constants, but only to be used when it makes sense.
Installation
Declaration
Documentation
getName()
Returns the name of the current value on EnumgetValue()
Returns the current value of the enumequals($enum)
Tests whether 2 enum instances are equal (returnstrue
if enum values are equal,false
otherwise)
Static methods:
toArray()
method Returns all possible values as an array (constant name in key, constant value in value)keys()
Returns the names (keys) of all constants in the Enum classvalues()
Returns instances of the Enum class of all Enum constants (constant name in key, Enum instance in value)search()
Return key for the searched value
Static methods
You can use phpdoc for autocompletion, this is supported in some IDEs:
Usage
This is how an enum can be defined.
This is how they are used:
This is how get we can get the enum name from a value:
Creating an enum from a value
Comparing enums
Enums can be compared using the equals
method:
You can also use dynamic is
methods:
Note that if you want auto completion on these is
methods, you must add extra doc blocks on your enum classes.
Search
You can search enum with its textual value: