PHP code example of morilog / php-enum

1. Go to this page and download the library: Download morilog/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/ */

    

morilog / php-enum example snippets




use Morilog\PhpEnum\Enum;

final class CommentStatus extends Enum
{
    const APPROVED = 1;
    const REJECTED = 2;
    const SOMETHING_ELSE = 3;
}




$status = new CommentStatus(1);
$status->key(); // APPROVED
$status->value(); // 1
$status->isApproved(); // true
$status->isRejected(); // false



$status = CommentStatus::rejected();
$status->key(); // REJECTED
$status->value(); // 2
$status->isApproved(); // false
$status->isRejected(); // true

$other = CommentStatus::somethingElse();
$other->key(); // SOMETHING_ELSE
$other->value(); // 3
$other->isApproved(); // false
$other->isRejected(); // false
$other->isSomethingElse(); // true



$status = CommentStatus::fromKey('APPROVED');



$status = CommentStatus::fromValue(1);



CommentStatus::keys(); // [APPROVED, REJECTED, SOMETHING_ELSE]

CommentStatus::values(); // [1, 2, 3]

CommentStatus::toArray(); // [APPROVED => 1, REJECTED => 2, SOMETHING_ELSE => 3]



$first = CommentStatus::approved();
$second = CommentStatus::rejected();
$third = CommentStatus::approved();

$first->equalsTo($second); // false
$first->equalsTo($third); // true
bash
composer