PHP code example of giritli / enum
1. Go to this page and download the library: Download giritli/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/ */
giritli / enum example snippets
final class StatusEnum extends Giritli\Enum\Enum {
const draft = 'draft';
const active = 'active';
const archived = 'archived';
const cancelled = 'cancelled';
protected $default = self::draft;
}
// This status defaults to draft as specified in the class
$status = new StatusEnum();
// You can instantiate an enum by value
$status = new StatusEnum('active');
$status = new StatusEnum(StatusEnum::active);
// Or by name
$status = StatusEnum::draft();
// Get the ordinal value of the enum
$status->getOrdinal(); // 0
// Get the value of the enum
$status->getValue(); // draft
echo $status;
// Get the key of the enum
$status->getKey(); // draft
// Get all values of an enum
$status->getValues();
StatusEnum::getValues();
// Get all ordinal values of an enum
$status->getOrdinals();
StatusEnum::getOrdinals();
// Get all key values of an enum
$status->getKeys();
StatusEnum::getKeys();