Download the PHP package eloquent/enumeration without Composer
On this page you can find all versions of the php package eloquent/enumeration. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download eloquent/enumeration
More information about eloquent/enumeration
Files in eloquent/enumeration
Package enumeration
Short Description An enumeration implementation for PHP.
License MIT
Homepage https://github.com/eloquent/enumeration
Informations about the package enumeration
No longer maintained
This package is no longer maintained. See this statement for more info.
Enumeration
An enumeration implementation for PHP.
Installation
- Available as Composer package eloquent/enumeration.
What is an Enumeration?
In terms of software development, an enumeration (or "enumerated type") is essentially a fixed set of values. These values are called "members" or "elements".
An enumeration is used in circumstances where it is desirable to allow an argument to be only one of a particular set of values, and where anything else is considered invalid.
A basic example
Enumeration can be used like C++ enumerated types. Here is an example, representing a set of HTTP request methods:
This class can now be used in a type hint to easily accept any valid HTTP request method:
Accessing enumeration members
Members are accessed by static method calls, like so:
For each member of the enumeration, a single instance of the enumeration class
is instantiated (that is, an instance of HttpRequestMethod
in the above
example). This means that strict comparison (===) can be used to determine
which member has been passed to a function:
Java-style enumerations
Java's enum types have slightly more functionality than C++ enumerated types. They can have additional properties and/or methods, and are really just a specialised kind of class where there are a fixed set of instances.
This is sometimes called the Multiton pattern, and in fact, all enumerations
in this implementation are Multitons. The AbstractEnumeration
class simply
defines its members based upon class constants.
Here is an example borrowed from the Java documentation for its enum types. The following multiton describes all of the planets in our solar system, including their masses and radii:
The above class can be used to take a known weight on earth (in any unit) and calculate the weight on all of the planets (in the same unit):
If the above script is executed, it will produce something like the following output:
Enumerations and class inheritance
When an enumeration is defined, the intent is usually to define a set of valid values that should not change, at least within the lifetime of a program's execution.
Since PHP has no in-built support for enumerations, this library implements them as regular PHP classes. Classes, however, allow for much more extensibility than is desirable in a true enumeration.
For example, a naive enumeration implementation might allow a developer to
extend the HttpRequestMethod
class from the examples above (assuming the
final
keyword is removed):
The problem with this scenario is that all the code written to expect only the
HTTP methods defined in HttpRequestMethod
is now compromised. Anybody can
extend HttpRequestMethod
to add custom values, essentially voiding the reason
for defining HttpRequestMethod
in the first place.
This library provides built-in protection from these kinds of circumstances. Attempting to define an enumeration that extends another enumeration will result in an exception being thrown, unless the 'base' enumeration is abstract.
Abstract enumerations
Assuming that there really is a need to extend HttpRequestMethod
, the way to
go about it is to define an abstract base class, then extend this class to
create the desired concrete enumerations:
In this way, when a developer uses a type hint for HttpRequestMethod
, there is
no chance they will ever receive the 'PATCH' method: