Enumerations

(PHP 8 >= 8.1.0)

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 Enumerations chapter.

Casting

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

add a note

User Contributed Notes 1 note

To Top

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant