PHP code example of paillechat / php-enum

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

    

paillechat / php-enum example snippets




use Paillechat\Enum\Enum;

/**
 * These docs are used only to help IDE
 * 
 * @method static static ONE
 * @method static static TWO
 */
class IssueType extends Enum 
{
    protected const ONE = 1;
    protected const TWO = 2;
} 

# Now you can create enum via named static call
/** @var Enum $one */
$one = IssueType::ONE();

# Enums keeps strict equality
$one1 = IssueType::ONE();
$one2 = IssueType::ONE();
$two = IssueType::TWO();

$one1 === $one2;
$one !== $two;

# Enums plays well with built-in functions
\in_array(IssueType::ONE(), [$one, $two], true);

# Enums plays well with signature type checks
function moveIssue(IssueType $type) {
    if ($type === IssueType::ONE()) {
        throw new \LogicException();
    }
    
    // ....
}

# You can convert enum to name and back
$name = $one->getName();
$new = IssueType::$name();

composer