PHP code example of vosiz / php-enum

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

    

vosiz / php-enum example snippets


use Vosiz\Enums\Enum;

class Elements extends Enum {

    public static function Init(): void {

        $vals = [
            'Fire' => 0,
            'Water' => 1,
            'Air' => 2,
            'Earth' => 3,
        ];
        self::AddValues($vals);
    } 
}

$elements = Elements::GetValues(); // all enum values - foreach usage f.e.

$armor->ResistanceType = Elements::GetEnumVal('Fire'); // allocate enum variable

if($armor->ResistanceType == Elements::GetEnumVal('Water')) { // comparation
    
    ...
}

$enum = Elements::GetEnum('Earth');

// access key or value
$key = $enum->GetKey(); // alt GetName()
$val = $enum->GetValue();

// compare - same enum class and same value
$is_water = $enum->Compare(Elements::GetEnum('Water'));

// if you know value but not name
$what_is_the_key = Elements::Find(2)->GetKey(); // Find returns enum or null

Elements::AddValues(['Ether' => 4]);

$ether = Elements::GetEnum('Ether');

class EdibleEnum extends Enum {

    public static function Init(): void {

        $vals = [
            'apple'     => 0,
            'leek'      => [100, 'pure poison'],
            'peach'     => 69,
            'pear'      => [1, 'apple relative'],

        ];
        self::AddValues($vals);
    } 
}

composer