Download the PHP package litgroup/enumerable without Composer
On this page you can find all versions of the php package litgroup/enumerable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download litgroup/enumerable
More information about litgroup/enumerable
Files in litgroup/enumerable
Package enumerable
Short Description Implementation of the enumerable type for PHP.
License MIT
Informations about the package enumerable
Enumerable
Library provides support of enumerable classes for PHP.
- Enumerable
- Installation
- Example of usage
- Definition
- Equality/Identity checking
- Usage in switch-case statement
- Serialization and Persistence
- Extensibility
- Run tests
- LICENSE
Installation
Install via composer:
Example of usage
Definition
- Create
final
class, which extendsEnumerable
; - For each variant of values create a static method, which
will creates an instance of value. For this purpose your method
must call
Enumerable::createEnum()
with some index of value.
Note:
- Enumerable class must be
final
!- Index can be of type
string
orint
.
Enum definition example:
Equality/Identity checking
You can use enumerable values in equality/identity expressions:
Note: Enumerables works as runtime constants. Therefor enumerable values can be checked on identity. And we recommend to use check on identity (
===
) instesd of equality (==
) if possible.
Usage in switch-case statement
Serialization and Persistence
Enumerable
works as runtime-constant. Enumerable type cannot be serialized.
If you need to store representation of enumerable in a database or send
it via an API you can use index of enumerable value as representation.
To restore an instance of enumerable type by index from database or
from API-request you can use static method getValueOf()
on the concrete
enum-class.
If you need to get all values of enumerable type, use static method
getValues()
on the concrete enum-class.
Extensibility
Instances of your enumerable classes can have additional behaviour if it needed.
But you cannot define any public static
methods with behaviour. Public static
methods used only for creation of values.
Note: You cannot define any
public static
methods with behaviour. Public static methods used only for creation of values.
Example:
Run tests
LICENSE
See LICENSE file.