Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

petrknap/php-enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-enum

Enumerated type for PHP by Petr Knap.

What is Enum?

In computer programming, an enumerated type (also called enumeration or enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an enumerated type has values that are different from each other, and that can be compared and assigned, but which are not specified by the programmer as having any particular concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily. -- Enumerated type - Wikipedia, The Free Encyclopedia

Why use Enums instead of Constants?

Because it is safer and less scary than using constants. Don't trust me? Let see at this code:

class MyBoolean
{
    const MY_TRUE = 1;
    const MY_FALSE = 2;
}

function isTrue(int $myBoolean)
{
    switch($myBoolean) {
        case MyBoolean::MY_TRUE:
            return true;
        case MyBoolean::MY_FALSE:
            return false;
    }
}

isTrue(MyBoolean::MY_TRUE);  // returns true - OK
isTrue(MyBoolean::MY_FALSE); // returns false - OK
isTrue(1);                   // returns true - OK
isTrue(2);                   // returns false - scary, but OK
isTrue(true);                // returns true - OK
isTrue(false);               // returns null - WTF?

And now the same code with Enum instead of Constants:

class MyBoolean extends \PetrKnap\Php\Enum\Enum
{
    protected function members()
    {
        return [
            "MY_TRUE" => 1,
            "MY_FALSE" => 2
        ];
    }
}

function isTrue(MyBoolean $myBoolean)
{
    switch($myBoolean) {
        case MyBoolean::MY_TRUE():
            return true;
        case MyBoolean::MY_FALSE():
            return false;
    }
}

isTrue(MyBoolean::MY_TRUE());  // returns true - OK
isTrue(MyBoolean::MY_FALSE()); // returns false - OK
isTrue(1);                     // uncaught type error - OK
isTrue(2);                     // uncaught type error - OK
isTrue(true);                  // uncaught type error - OK
isTrue(false);                 // uncaught type error - OK

Usage of php-enum

Enum declaration

class DayOfWeekEnum extends \PetrKnap\Php\Enum\Enum
{
    protected function members()
    {
        return [
            "SUNDAY" => 0,
            "MONDAY" => 1,
            "TUESDAY" => 2,
            "WEDNESDAY" => 3,
            "THURSDAY" => 4,
            "FRIDAY" => 5,
            "SATURDAY" => 6
        ];
    }
}

Enum usage

if (DayOfWeekEnum::FRIDAY() == DayOfWeekEnum::FRIDAY()) {
    echo "This is OK.";
}
if (DayOfWeekEnum::FRIDAY() == DayOfWeekEnum::MONDAY()) {
    echo "We are going to Hell!";
}
function isWeekend(DayOfWeekEnum $dayOfWeek)
{
   switch ($dayOfWeek) {
       case DayOfWeekEnum::SATURDAY():
       case DayOfWeekEnum::SUNDAY():
           return true;
       default:
           return false;
   }
}
if (date('w') == DayOfWeekEnum::FRIDAY()->getValue()) {
    echo "Finally it is Friday!";
}

How to install

Run composer require petrknap/php-enum or merge this JSON code with your project composer.json file manually and run composer install. Instead of dev-master you can use one of released versions.

{
    "require": {
        "petrknap/php-enum": "dev-master"
    }
}

Or manually clone this repository via git clone https://github.com/petrknap/php-enum.git or download this repository as ZIP and extract files into your project.

About

Enumerated type for PHP

Topics

Resources

License

LGPL-3.0, GPL-3.0 licenses found

Licenses found

LGPL-3.0
COPYING.LESSER
GPL-3.0
COPYING

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published