1. Go to this page and download the library: Download ynkt/enum-like 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/ */
ynkt / enum-like example snippets
use Ynkt\Enum\Enum;
/**
* Class Status
*/
class Status extends Enum
{
private const READY = 'Ready';
private const IN_PROGRESS = 'In Progress';
private const DONE = 'Done';
private string $text;
// The argument of __constructor() is automatically assigned to the value of the enumerator
protected function __construct(string $text) { $this->text = $text; }
public function text(): string { return $this->text; }
}
// Provided by this library
Status::values(); // Returns instances of the Enum class of all Enumerators
Status::first(); // Returns the first instance
Status::first(callable $closure); // Returns the first instance that passes a given truth
Status::has(callable $closure); // Tests an instance exists that passes a given truth
// Provided by the user declaration
// This library automatically provide the static method whose name is same of the enumerators name.
// The following methods return an Enum instance.
Status::READY();
Status::IN_PROGRESS();
Status::DONE();
$status = Status::READY();
// Provided by this library
$status->name(); // Returns the name of the current enumerator (e.g.:'READY')
$status->ordinal(); // Returns the ordinal of the current enumerator (e.g.:0)
$status->declaringClass(); // Returns the declaring class of the current enumerator (e.g.:'Status')
$status->equals(Status::Ready()); // Tests enum instances are equal (e.g.:true)
// Provided by the user declaration
$status->text(); // Returns 'Ready'
function updateStatus(Status $status){
// ...
}
updateStatus(Status::READY());
class Color extends Enum
{
private const RED = ['#FF0000', [255, 0, 0]];
private const BLUE = ['#0000FF', [0, 0, 255]];
private const BLACK = ['#000000', [0, 0, 0]];
// The argument of __constructor() is automatically assigned to the value of the enumerator
protected function __construct(string $code, array $rgb) {}
// ...
}
class Color extends Enum
{
private const RED = ['#FF0000', [255, 0, 0]];
private const BLUE = ['#0000FF', [0, 0, 255]];
private const BLACK = ['#000000', [0, 0, 0]];
// ...
}
class DayOfWeek extends Enum
{
use ByIdTrait;
private const MONDAY = 1;
private const TUESDAY = 2;
private int $id;
public function id(): int { return $this->id; }
// ...
}
// You can use byId() to get the Enum instance.
$dayOfWeek = DayOfWeek::byId(1);
$dayOfWeek->equals(DayOfWeek::MONDAY()); // Returns true
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.