CakeFest 2024: The Official CakePHP Conference

列挙型と定数

列挙型には定数も含めることができます。 定数には public, private, protected が指定できますが、 列挙型では実際 private と protected は同じものです。 なぜなら、継承が許されていないからです。

列挙型の定数は、以下のように case を参照していても構いません:

<?php

enum Size
{
case
Small;
case
Medium;
case
Large;

public const
Huge = self::Large;
}
?>
add a note

User Contributed Notes 1 note

up
8
Hayley Watson
6 months ago
Just to clarify, enum constants *can* contain cases, but they don't *have* to; other constant values are legitimate - including cases of other Enumerations.

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

public const
Card = Size::Large; // A case from a different enum
}

enum
Size
{
case
Small;
case
Medium;
case
Large;

public const
Scale = 297/210; // A float
}

echo
Suit::Diamonds::Card::Scale; // Getting the constant Scale from the constant Card in a Suit.
?>
To Top