vosiz/php-enum

PHP simple enum-like class

v2.2.1 2025-03-15 22:25 UTC

This package is auto-updated.

Last update: 2025-06-15 22:57:20 UTC


README

Utility to have enum-like classes. We love ENUMS! True enumeration with integers seems to be impossible to achieve (at least for me). Designed to be dynamical (not only for compile time - read values from DB f.e. and make dynamic enums)

Bug report

None taken

Installation

Composer

Install with

composer install vosiz/php-enum

Update with (dependencies/required)

composer update

Usage

Implement your own enums! Lets say, you have a game and want elemental magic defined other way than with defines or constants. You want to do something like this:

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

Important

You can multiple same values allocated to diferent keys (as some classic enum does not allow it) - here it is permitted. But you can lose consistency if you base on integers (values) too much.

Usage sample - int-like

Now you can simply use it for example like this. It is value based - integers.

$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
    
    ...
}

Usage sample - enum-like

If you need to track or operate with naming of enum (enum key and value in general), you should use something like this:

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

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

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

// i fyou know value but not name
$what_is_the_key = Elements::Find('Air'); 

Useful tips

If you want to distinct 'key' from 'sho key', there is Display property. It is automatically set to key, but you can set it up in custom enum definition. Example.

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

Important

You have to still keep first value of array as int (core functionality of enum).