Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

Enumerations (enum)

Reference for PHP 8.1 enums: pure and backed enums, cases(), from() and tryFrom(), methods, constants, interfaces and where enums can be used.

An enum (added in PHP 8.1) is a type with a fixed set of named values called cases. Each case is a single object, so Suit::Hearts === Suit::Hearts is always true and a function that accepts Suit can never receive an invalid value.

A pure enum has only case names. A backed enum gives every case a scalar value, either all int or all string, which makes it easy to store in a database or send as JSON. Enums can have methods, constants and interfaces, but no properties and no state.

PHP
enum Status: string
{
    case Draft = "draft";
    case Published = "published";
}

Try it

PHP Pure and backed enums

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP from() errors, match and constants

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
enum Name { case A; case B; }
enum Name: string|int implements Interface
{
    case A = "value";
    const X = self::A;
    public function method(): mixed { }
    public static function make(): self { }
}
Member Description
Name::A The case object. It has a read-only name property, and value on backed enums.
Name::cases() Static method returning an array of all cases in declaration order.
Name::from($value) Backed enums only. Returns the case or throws ValueError.
Name::tryFrom($value) Backed enums only. Returns the case or null.

Errors: from() throws ValueError for an unknown value. Using new or clone on an enum throws an Error.

Usage notes

  • Identity, not equality of data. Compare cases with ===. Cases are singletons.
  • Backing values must be unique literals of one type (int or string); every case of a backed enum needs a value.
  • No properties, no state. Enums can have methods, static methods, constants and traits (without properties), and can implement interfaces.
  • Type declarations. Use the enum name as a parameter, return or property type to reject invalid values at the boundary.
  • Constant expressions. Cases can be used in constants, default parameter values and attribute arguments (Status::Draft).
  • Not int. An enum case does not convert to its value automatically; use ->value. Backed enums also work in match.
  • JSON. json_encode() of a backed enum gives its value. Pure enums cannot be encoded and throw.
  • Fixed set. Enums cannot be extended. Use an interface if you need several enums to share a contract.