Skip to content
PHP7 Enum library
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitignore
.travis.yml
LICENSE
README.md
composer.json
composer.lock

README.md

Build Status Twitter LinkedIn

php7-enum

PHP7 Enum library

This library create ValueObject for Enum lists

Installation

composer require jeckel/php7-enum

Usage

To declare new Enum list, just create a new class that extends the EnumAbstract and declare possible values as constants

<?php
/**
 * Class StatusEnum
 *
 * @method static DRAFT(): StatusEnum
 * @method static VALID(): StatusEnum
 * @method static DELETED(): StatusEnum
 */
class StatusEnum extends PHP7Enum\EnumAbstract
{
    const DRAFT = 'draft';
    const VALID = 'valid';
    const DELETED = 'deleted';
}

Now you can get valueObject as statics methods.

All instances of the same item are pointer to the same object and then identical.

<?php

$draft = StatusEnum::DRAFT();
$valid = StatusEnum::VALID();

echo ($draft instanceof StatusEnum::class) ? 'true' : 'false'; // true
echo $draft; // draft

// 2 different calls return the same instance
echo ($draft === StatusEnum::DRAFT()) ? 'true' : 'false'; //true

?>

Run tests

docker run -v `pwd`:/project --rm -it --workdir /project php:alpine php vendor/bin/phpunit --bootstrap vendor/autoload.php tests
You can’t perform that action at this time.