PHP code example of abhi1693 / yii2-enum

1. Go to this page and download the library: Download abhi1693/yii2-enum library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

abhi1693 / yii2-enum example snippets


use abhimanyu\enum\helpers;

class Month extends BaseEnum
{
    const January = 1;
    const February = 2;
    const March = 3;
    const April = 4;
    const May = 5;
    const June = 6;
    const July = 7;
    const August = 8;
    const September = 9;
    const October = 10;
    const November = 11;
    const December = 12;
}

// static call
$month = Month::May();

// direct instantiation
$month = new Month(Month::May);

// by value
$month = Month::createByValue(5);

// by name
$month = Month::createByName('May');

$name = $month->getName();
$value = $month->getValue();

if (Month::isValidName('May')) {
    // it is valid
}

if (Month::isValidValue($value)) {
    // it is valid
}

$constantsByName = Month::getConstantsByName();

echo $constantsByName['May']; // 5

$constantsByValue = Month::getConstantsByValue();

echo $constantsByValue[5]; // "May"