Enum types are represented by classes. Values of specific enum type are represented by @method annotations that enables code completion and doesn't cause duplicity.
<?php
require_once('vendor/autoload.php');
/**
* @method static TypeEnum TYPE_1()
* @method static $this TYPE_2()
*/
final class TypeEnum extends \AL\PhpEnum\Enum {}
var_dump((string)TypeEnum::TYPE_2()); // string(6) "TYPE_2"
var_dump(TypeEnum::TYPE_2()->getValue()); // string(6) "TYPE_2"
var_dump(TypeEnum::TYPE_1()->getOrder()); // int(0)
var_dump(TypeEnum::TYPE_2()->getOrder()); // int(1)
var_dump(TypeEnum::TYPE_2() === TypeEnum::TYPE_2()); // bool(true)
var_dump(TypeEnum::TYPE_2() == TypeEnum::TYPE_2()); // bool(true)
var_dump(TypeEnum::TYPE_1() === TypeEnum::TYPE_2()); // bool(false)
var_dump(TypeEnum::TYPE_1() == TypeEnum::TYPE_2()); // bool(false)
var_dump(TypeEnum::parse('TYPE_2') === TypeEnum::TYPE_2()); // bool(true)
var_dump(TypeEnum::tryParse('NOT_EXIST')); // NULL
var_dump(TypeEnum::inOrder(0) === TypeEnum::TYPE_1()); // bool(true)
var_dump(TypeEnum::tryInOrder(1000)); // NULL
The code needs PHP 7.1 or greater.
composer require adam-lutka/php-enum
composer install
vendor/bin/phpunit
We use SemVer for versioning. For the versions available, see the tags on this repository.