PHP.nl

Enumerations

Enumerations

Basic Enumerations

Enumerations are a restricting layer on top of classes and class constants, intended to provide a way to define a closed set of possible values for a type.

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

function do_stuff(Suit $s)
{
    // ...
}

do_stuff(Suit::Spades);
?>

For a full discussion, see the chapter. Enumerations

Casting

If an is converted to an , it is not modified. If an is converted to an , an array with a single key (for Pure enums) or an array with both and keys (for Backed enums) is created. All other cast types will result in an error. enum``object``enum``array``name``name``value