PHP code example of jgswift / kenum

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

    

jgswift / kenum example snippets



class MyEnum extends kenum\Enum\Base {
    const Option1 = 'Option1';
    const Option2 = 'Option2';
}

$enum = new MyEnum(MyEnum::Option2);

// get current enum value with the value method or through string conversion
$value = $enum->value()  // Returns 'Option1'
$string = (string)$enum; // Returns 'Option1'

// check for equality
$equals = $enum->equals(MyEnum::Option2); // returns true
$equals = $enum->equals(new MyEnum(MyEnum::Option1)); // returns false


class MyEnum extends kenum\Enum\Bitwise {
    const Option1 = 1;
    const Option2 = 2;
    const Option3 = 4;

    /* etc... */
}

$enum = new MyEnum(MyEnum::Option2 | MyEnum::Option3);

// get current enum value with the value method or through string conversion
var_dump($enum->value());  // Returns '6'
var_dump((string)$enum); // Returns 'Option2 Option3'

// check for equality
var_dump($enum->equals(MyEnum::Option2)); // returns false
var_dump($enum->equals(MyEnum::Option2 | MyEnum::Option3)); // returns true

// check for flag
var_dump($enum->hasFlag(MyEnum::Option2)); // returns true
var_dump($enum->hasFlag(MyEnum::Option1)); // returns false

class MyEnum extends kenum\Enum\Bitwise {
    const Option1 = 1;
    const Option2 = self::Option1 * 2; // 2
    const Option3 = self::Option2 * 2; // 4
    const Option4 = self::Option4 * 2; // 8
    /* etc... */
}

class MyEnum {
    use kenum\Enum;

    function __construct($value) {
        /* store value(s) */
    }

    // custom equality check
    function equals($value) {
        /* check for equality */
    }

    function __toString() {
        /* transform enum value(s) into human-readable text */
    }
}
sh
php composer.phar