PHP code example of ll-kuma-ll / php-enum

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

    

ll-kuma-ll / php-enum example snippets


namespace Foo;

use LLkumaLL\Enum\Enum;

class Sample extends Enum
{
    const ENUM = [
        'VALUE_1' => 'label 1',
        'VALUE_2' => 'label 2',
    ];
}

use LLkumaLL\Enum\Manager;
use Foo\Sample;

// 単独で使いたい場合
$enum = Sample::VALUE_1();
// 'label 1' が出力される
echo $enum->label();
// 'VALUE_1' が出力される
echo $enum->value();

// まとめて取り扱いたい場合
$manager = new Manager(Sample::class);

// ENUM定数配列の定義分全部をループ処理
foreach ($manager->createAll() as $const => $enum) {
    // '同じ'が出力される
    echo $const == $enum->value() ? '同じ' : '違う';
}
bash
php composer.phar