International PHP Conference Munich 2025

Enumeration constants

Enumerations may include constants, which may be public, private, or protected, although in practice private and protected are equivalent as inheritance is not allowed.

An enum constant may refer to an enum case:

<?php

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

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

User Contributed Notes 1 note

up
9
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.

<?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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy