ReflectionEnum::getCase

列挙型の特定の case を返す

説明

public ReflectionEnumUnitCase ReflectionEnum::getCase(string $name)

列挙型の特定の case を名前で指定すると、 リフレクションオブジェクトを返します。 指定された case が定義されていない場合、 ReflectionException がスローされます。

パラメータ

name

取得する case の名前。

戻り値

ReflectionEnumUnitCase または ReflectionEnumBackedCase のインスタンスを適切に返します。

例1 ReflectionEnum::getCase の例

<?php
enum Suit
{
    case 
Hearts;
    case 
Diamonds;
    case 
Clubs;
    case 
Spades;
}

$rEnum = new ReflectionEnum(Suit::class);

$rCase $rEnum->getCase('Clubs');

var_dump($rCase->getValue());
?>

上の例の出力は以下となります。

enum(Suit::Clubs)

参考

  • 列挙型(Enum)
  • ReflectionEnum::getCases
  • ReflectionEnum::hasCase
  • ReflectionEnum::isBacked