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);
}
}
$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