PHP code example of buuum / phenum

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

    

buuum / phenum example snippets


final class UserEnum extends \PhEnum\PhEnum
{
    const SEX_MALE = 1;
    const SEX_FEMALE = 2;

}

$enum = new UserEnum(UserEnum::SEX_MALE);
$enum_female = new UserEnum(UserEnum::SEX_FEMALE);

$enum->equals($enum_female); // (false)
$enum->getValue(); // (1)
$enum->getKey(); // (SEX_MALE)

UserEnum::SEX_MALE(); // instance of PhEnum

UserEnum::isValidKey('SEX_MALE'); // (true)
UserEnum::isValid(5); // (false)
UserEnum::search(UserEnum::SEX_FEMALE); // (SEX_FEMALE)
UserEnum::toArray(); // (array(2) { ["SEX_MALE"]=> int(1) ["SEX_FEMALE"]=> int(2) })
UserEnum::keys(); // (array(2) {
                        [0]=>
                        string(8) "SEX_MALE"
                        [1]=>
                        string(10) "SEX_FEMALE"
                      })
UserEnum::values(); // array(2) {
                         [0]=>
                         int(1)
                         [1]=>
                         int(2)
                       }

function demo(UserEnum $enum)
{
    return $enum->getValue();
}
demo(new UserEnum(UserEnum::SEX_MALE));
// or
demo(UserEnum::SEX_MALE());

function demo($enum)
{
    if(!UserEnum::isValid($enum)){
        throw new Exception("Value not valid");
    }
    return false;
}
demo(UserEnum::SEX_FEMALE);