PHP code example of wirus15 / enum

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

    

wirus15 / enum example snippets


use Enum\Enum;

// defining our Enum class
final class Example extends Enum
{
    const FOO = 1;
    const BAR = 2;
    const YOLO = 3;
}

$foo = Example::get(Example::FOO);
$bar = Example::get('bar');
$yolo = Example::YOLO();

function test(Example $enum) 
{
    if ($enum->is(Example::FOO)) {
        echo 'foo';
    } else if ($enum->is(Example::BAR)) {
        echo 'bar';
    } else {
        echo $enum;
    }
}

test($foo);   // foo
test($bar);   // bar
test($yolo);  // Yolo


Example::get(Example::FOO);               // instance of Example
Example::FOO();                           // shortcut
Example::all();                           // array of Example instances

// keys and values
Example::keys();                          // array of keys ['FOO', 'BAR', 'YOLO']
Example::values();                        // array of values [1, 2, 3]

// checking if value exists
Example::has(3);                          // true
Example::has(4);                          // false

$foo = Example::FOO();
$foo->key();                              // 'FOO'
$foo->value();                            // 1

$foo = Example::FOO();
$foo->is(1);                              // true
$foo->is(Example::FOO);                   // true
$foo->is($foo);                           // true

// comparing with strict option (type comparison)
$foo->is($foo, true);                     // true
$foo->is(1, true);                        // false

// searching in array
$foo->in([1,2,3]);                        // true
$foo->in([2,3]);                          // false

Enum::get(Example::FOO, Example::class);
Enum::all(Example::class);    
Enum::keys(Example::class);      
Enum::values(Example::class); 
Enum::has(1, Example::class);