Constantes d'énumération

Les énumérations peuvent inclure des constantes, qui peuvent être publiques, privées ou protégées, bien qu'en pratique, privées et protégées soient équivalentes car l'héritage n'est pas autorisé.

Une constante d'énumération peut faire référence à un cas d'énumération :

<?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
1 year 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.<?phpenum 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