定義
<?php
enum Color: int
{
case RED = 1;
case BLUE = 2;
case GREEN = 3;
public function label(): string
{
return match ($this) {
self::RED => '赤',
self::BLUE => '青',
self::GREEN => '緑',
};
}
public function katakanaLabel(): string
{
return match ($this) {
self::RED => 'アカ',
self::BLUE => 'アオ',
self::GREEN => 'ミドリ',
};
}
}
利用
<?php
require_once './Color.php';
foreach (Color::cases() as $color) {
echo "<p>" . $color->value . ":" . $color->label() . ":" . $color->katakanaLabel() . "</p>";
}
コメント