PHP code example of php-compatible / enum

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

    

php-compatible / enum example snippets




use PhpCompatible\Enum\Enum;
use PhpCompatible\Enum\Value;

/**
 * @method static Value hearts()
 * @method static Value diamonds()
 * @method static Value clubs()
 * @method static Value spades()
 */
class Suit extends Enum
{
    protected $hearts;   // 0
    protected $diamonds; // 1
    protected $clubs;    // 2
    protected $spades;   // 3
}

echo Suit::hearts()->name;  // "hearts"
echo Suit::hearts()->value; // 0

class Status extends Enum
{
    protected $draft;        // 0
    protected $pending;      // 1
    protected $published = 10;
    protected $archived;     // 11
}

class Color extends Enum
{
    protected $red = 'red';
    protected $green = 'green';
    protected $blue = 'blue';
}

echo Color::red()->name;  // "red"
echo Color::red()->value; // "red"

class Status extends Enum
{
    protected $pendingReview;
}

// All of these return the same Value instance:
Status::pendingReview();   // camelCase
Status::PendingReview();   // PascalCase
Status::PENDING_REVIEW();  // SCREAMING_SNAKE_CASE
Status::pending_review();  // snake_case

// The name property preserves the original definition:
Status::PENDING_REVIEW()->name; // "pendingReview"

foreach (Suit::cases() as $case) {
    echo $case->name . ': ' . $case->value . PHP_EOL;
}
// hearts: 0
// diamonds: 1
// clubs: 2
// spades: 3

// from() throws an exception if the value doesn't exist
$case = Suit::from(0);
echo $case->name;  // "hearts"

// tryFrom() returns null if the value doesn't exist
$case = Suit::tryFrom(999);
var_dump($case);  // null

// Type-sensitive: integer 0 won't match string '0'
$case = Suit::tryFrom('0');
var_dump($case);  // null

// This will throw LogicException
class Invalid extends Enum
{
    protected $foo = 1;
    protected $bar = 1;  // Duplicate value!
}

use PhpCompatible\Enum\EnumLabel;

class TaskStatus extends Enum
{
    protected $pendingReview;
    protected $inProgress;
    protected $onHold;
}

echo EnumLabel::from(TaskStatus::pendingReview()); // "Pending Review"
echo EnumLabel::from(TaskStatus::inProgress());    // "In Progress"
echo EnumLabel::from(TaskStatus::onHold());        // "On Hold"

$label = EnumLabel::from(TaskStatus::pendingReview());

echo "Status: $label";    // "Status: Pending Review"
echo $label->toString();  // "Pending Review"

/**
 * @method static Value hearts()
 * @method static Value diamonds()
 * @method static Value clubs()
 * @method static Value spades()
 */
class Suit extends Enum
{
    protected $hearts;
    protected $diamonds;
    protected $clubs;
    protected $spades;
}

use PhpCompatible\Enum\Enum;
use PhpCompatible\Enum\EnumLabel;

class Status extends Enum
{
    protected $draft;
    protected $published = 10;
}

$status = Status::draft();
echo EnumLabel::from($status);

use PhpCompatible\Enum\Php8EnumLabel;

enum Status: int
{
    case draft = 0;
    case published = 10;
}

$status = Status::draft;
echo Php8EnumLabel::fromEnum($status);

// PHP 8.1+
enum Status {
    case PendingReview;
    case InProgress;
}

echo Php8EnumLabel::fromEnum(Status::PendingReview); // "Pending Review"
bash
composer