PHP code example of vaened / php-enum

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

    

vaened / php-enum example snippets


$status = Status::WARNING();

$status->key(); // WARNING
$status->value(); // Advertencia

// custom attribute
$status->getColor(); // yellow

 namespace App\Enums;

use App\Color;
use Vaened\Enum\Attributor;
use Vaened\Enum\Enum;

class Status extends Enum
{
    // Enums
    public const WARNING = 'Advertencia';

    public const SUCCESS = 'Exito';
}

use App\Enums\Status;

// returns an instance of Status, with the value of the constant WARNING
Status::WARNING();

class Status extends Enum
{
    public const WARNING = 'Advertencia';

    public static function WARNING(): self 
    {
        return self::create(self::WARNING);
    }
}

/**
 * Class Status
 *
 * @method static Status WARNING()
 */
class Status extends Enum
{
    public const WARNING = 'Advertencia';
}
 
Attributor::to('CONSTANT', [
    'name' => 'value'
]);

class Status extends Enum
{
    public const SUCCESS = 'Éxito';

    public function getColor(): Color
    {
        return $this->attribute('color');
    }

    protected static function attributes(): array
    {
        return [
            Attributor::to('SUCCESS', [
                'color' => new Color('blue'),
            ]),
        ];
    }
}