Skip to content

Latest commit

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Enum

Build StatusScrutinizer Code QualityCode CoverageLatest Stable VersionTotal Downloads

A PHP 7+ enumeration library.

Why?

To create perfect enums for PHP library

Installation

composer require "paillechat/php-enum:^2.0"

Usage

Declare enum class by extending basic Enum and filling it with constants. Constant value does not matter. You can fill it with any payload you can utilize as general constant, but we suggest you to keep constants as protected as possible

<?phpusePaillechat\Enum\Enum;
/** * These docs are used only to help IDE *  * @method static static ONE * @method static static TWO */class IssueType extends Enum {
protectedconstONE = 1;
protectedconstTWO = 2;
} # Now you can create enum via named static call/** @var Enum $one */$one = IssueType::ONE();
# Enums keeps strict equality$one1 = IssueType::ONE();
$one2 = IssueType::ONE();
$two = IssueType::TWO();
$one1 === $one2;
$one !== $two;
# Enums plays well with built-in functions\in_array(IssueType::ONE(), [$one, $two], true);
# Enums plays well with signature type checksfunctionmoveIssue(IssueType$type) {
if ($type === IssueType::ONE()) {
thrownew \LogicException();
}
// ....
}
# You can convert enum to name and back$name = $one->getName();
$new = IssueType::$name();

About

Enum implementation for PHP 7

Resources

Stars

16 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages