1. Go to this page and download the library: Download bohacpetr/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/ */
bohacpetr / enum example snippets
use bohyn\Enum\Enum;
// To create new enum, you just have to define constants
class MyEnum extends Enum {
public const STATUS_NEW = 1;
public const STATUS_ACTIVE = 2;
public const STATUS_INACTIVE = 3;
public const STATUS_DELETED = 4;
}
MyEnum::getValidValues(); // [1, 2, 3, 4]
$myEnum = new MyEnum(5); // throws bohyn\Enum\EnumException
$myEnum = new MyEnum(MyEnum::STATUS_ACTIVE);
// returns enum value
$myEnum->get(); // 1
// comparisons can be by value or by instance
$myEnum->equals(1); // true
$myEnum->equals(2); // false
$myEnum->equals(new MyEnum(MyEnum::STATUS_DELETED)); // false
// Check if enum value is one of values in array
$myEnum->equalsAny([1, 2]); // true
$myEnum->equalsAny([2, 3, 4]); // false
(string)$myEnum; // 1
MyEnum::isValid(1); // true
MyEnum::isValid(5); // false