PHP code example of spareparts / enum

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

    

spareparts / enum example snippets


/**
 * @method static OPEN
 * @method static CLOSED
 */
class WindowStateEnum extends \SpareParts\Enum\Enum
{
}

// obtain enum value
$state = WindowStateEnum::OPEN();

// assign enum value
$windows->state = WindowStateEnum::CLOSED();

// compare enum values
if ($window->state === WindowStateEnum::OPEN()) {
    ....
}

// use enum to guard method parameters
function changeWindowState(WindowStateEnum $newState) {
    ...
}


$state = WindowStateEnum::OPEN();

/**
 * @method static OPEN
 * @method static CLOSED
 */
class WindowStateEnum extends \SpareParts\Enum\Enum 
{
}

$state = WindowStateEnum::instance('OPEN');

try {
    $window->setState(WindowStateEnum::FLYING());
} catch (InvalidEnumValueException $e) {
    echo "This is not a correct state for window to be in!";
}

if ($state->isAnyOf([Window::OPEN(), Window::CLOSED()]));