Dead simple PHP enum. This thing runs on PHP 5.3, heck maybe even PHP 5.1.2*, so just yoink the files if you need it for your Legacy project.
* This enum uses the Reflection class, which was introduced in PHP 5.1.2
I come from C-like languages with proper enums. Why is there no such simple, built-in, proper, enum in PHP?
The SplEnum looks promising, but has some oddities with its implementation. As an added bonus, it might not work for your setup.
Other third-party alternatives (see this, this, this, and this for instance) are overly complicated for my needs. This should probably solve it for most of my use cases.
<?php
use SimpleEnum\Enum;
class MyEnum extends Enum
{
const foo = 1;
const bar = 2;
const baz = 3;
}
$myEnum = new MyEnum(MyEnum::foo);
assert($myEnum->value() === MyEnum::foo);
assert($myEnum->equals(new MyEnum(1)));
composer require vladdesv/simple-enum
MIT © Vladimirs Nordholm