Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Php

Idiom #122 Declare an enumeration

Create an enumerated type Suit with 4 possible values SPADES, HEARTS, DIAMONDS, CLUBS.

abstract class Suit
{
  const SPADES	 = 0;
  const HEARTS	 = 1;
  const DIAMONDS = 2;
  const CLUBS	 = 3;
}

PHP lacks enumeration, but the functionality can be emulated using constants.
enum Suit
{
    case SPADES;
    case HEARTS;
    case DIAMONDS;
    case CLUBS;
}

Will be available starting PHP 8.1
type Suit is (Spades, Hearts, Diamonds, Clubs);

New implementation...
< >
programming-idioms.org