PHP code example of uginroot / php-enum

1. Go to this page and download the library: Download uginroot/php-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/ */

    

uginroot / php-enum example snippets


use Uginroot\PhpEnum\Choice;
use Uginroot\PhpEnum\EnumAbstract;

class Month extends EnumAbstract {
    public const January = 1;
    public const February = 2;
}

$jun = new Month(Month::January);
$junName = Month::createByName('January');
$junValue = Month::createByValue(Month::January);

$jun->getValue(); // 1
$jun->getName();  // 'January'
$name = (string)$jun; // 'January'

$jun->is($junName);            // true
$jun->is($junValue);           // true
$jun->isName('January');       // true
$jun->isValue(Month::January); // true

// Immutable
$jun->setValue(Month::February); // new Month(Month::February)
$jun->setName('February'); // Month::createByName('February')

// Name and value variants
/** @var Choice $choice */
$choice = Month::getChoice();
$choice->getValues(); // [1, 2]
$choice->getNames();  // ['January', 'February']
$choice->getValue('January'); // 1
$choice->getName(Month::January); // 'January'
$choice->isValidName('January'); // true
$choice->isValidValue(Month::January); // true
bash
composer