PHP code example of mle86 / enum

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

    

mle86 / enum example snippets



class TriState extends \mle86\Enum\AbstractAutoEnum
{
    public const HIGH  = 'H';
    public const LOW   = 'L';
    public const UNDEF = 'Z';
}


$state = new TriState(TriState::HIGH);

var_dump($state->value());     // H
var_dump(json_encode($state)); // "H"
var_dump($state->equals(TriState::HIGH)); // true
var_dump($state->equals(TriState::LOW));  // false

function (TriState $state) {
    // $state is definitely a TriState instance here,
    // so it definitely wraps a valid TriState constant.
}


function ($state, $optionalState = null) {
    TriState::validate($state);
    // Now we can be sure that $state contains a valid TriState value
    // (or maybe it's even a TriState instance).
    
    TriState::validateOptional($optionalState);
    // Now we know that $optionalState contains either
    // a valid TriState value,
    // a valid TriState instance,
    // or NULL.
}