Skip to content

A PHP library providing a way to work with enumeration

License

Notifications You must be signed in to change notification settings

Klamius/php-enum

Repository files navigation

PHP Enum inspired by SplEnum

Build Status Code Coverage Scrutinizer Code Quality Total Downloads Latest Stable Version

Installation

by using composer

composer require klamius/php-enum

Philosophie

This library give an easier way to emulate and create enumeration objects natively in PHP and be replacement to SplEnum which is not integrated directly to PHP.

In our daily usage we deal with many enums (all domain entity states, months, genders, etc. ) and we get into dilemma, should it be Class member, constants or Interface constants and so on. and then we treat it as a scalar value which most of time can't be validated or type hinted.

So using an enum instead of constants provides the following advantages:

  • type hint: function setState(OrderStateEnum $state) {

Declaration

use Klamius\Enum\Enum;

/**
 * GenderEnum enum
 */
class Gender extends Enum
{
    const MALE = 'male';
    const FEMALE = 'female';
}

Usage

class User
{
    /**
     * @var Gender
     */
    private $gender;
    
    function setGender(Gender $gender)
    {
        $this->gender = $gender;
    }
}

$gender = new Gender(Gender::MALE);
$user->setGender($gender);

//or
echo $gender;

About

A PHP library providing a way to work with enumeration

Resources

License

Stars

Watchers

Forks

Packages

No packages published