dasprid/enum
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The dasprid/enum is a PHP 7.1 package that provides an efficient implementation of enumerations. PHP, by nature, does not have a robust built-in enum support. Although it does offer SplEnum
implementation as a part of the PECL extension, it is not complete and necessitates the use of userland implementations. The dasprid/enum package bridges this gap by providing a sound and reliable enum implementation, operating with constants and allowing for more manageable code.
To use the dasprid/enum package, you first need to include it in your project using composer. From there, you can create enums by subclassing the DASPRiD\Enum\AbstractEnum
class and defining the constants with the protected scope. It's best to illustrate this with an example code:
use DASPRiD\Enum\AbstractEnum;
/**
* @method static self MONDAY()
* @method static self TUESDAY()
* @method static self WEDNESDAY()
* @method static self THURSDAY()
* @method static self FRIDAY()
* @method static self SATURDAY()
* @method static self SUNDAY()
*/
final class WeekDay extends AbstractEnum
{
protected const MONDAY = null;
protected const TUESDAY = null;
protected const WEDNESDAY = null;
protected const THURSDAY = null;
protected const FRIDAY = null;
protected const SATURDAY = null;
protected const SUNDAY = null;
}
In this example, a WeekDay enum is defined with constants for each day of the week. To use these enums, simply call them like functions. They are singletons, which guarantees there is always only one instance of the same type:
function tellItLikeItIs(WeekDay $weekDay)
{
switch ($weekDay) {
case WeekDay::MONDAY():
echo 'Mondays are bad.';
break;
case WeekDay::FRIDAY():
echo 'Fridays are better.';
break;
case WeekDay::SATURDAY():
case WeekDay::SUNDAY():
echo 'Weekends are best.';
break;
default:
echo 'Midweek days are so-so.';
}
}
tellItLikeItIs(WeekDay::MONDAY());
tellItLikeItIs(WeekDay::WEDNESDAY());
As for the documentation, it can be found in the readme file located within the dasprid/enum repository on GitHub. The readme delivers a thorough introduction to this package, along with its description, usage examples, and detailed code snippets that showcase the package's functionalities and how to implement them effectively. Simply visit the repository's URL to access this information: https://github.com/DASPRiD/Enum.git.