PHP code example of emulgeator / enum

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

    

emulgeator / enum example snippets


use Emul\Enum\EnumAbstract;

class Status extends EnumAbstract
{
    const ENABLED  = 'enabled';
    const DISABLED = 'disabled';
    const DELETED  = 'deleted';

    public static function enabled(): self
    {
        return new self(self::ENABLED);
    }

    public static function disabled(): self
    {
        return new self(self::DISABLED);
    }

    public static function deleted(): self
    {
        return new self(self::DELETED);
    }

    protected static function getPossibleValues(): array
    {
        return [
            self::ENABLED,
            self::DISABLED,
            self::DELETED,
        ];
    }
}

$enabled  = Status::enabled();
$disabled = Status::disabled();
$deleted  = Status::createFromString('invalid'); // Throws exception

$enabled->isEqualToString('disabled'); // false
$enabled->isEqualTo($disabled); // false