[PHP-NOTES] note 130472 added to unitenum.cases

[email protected] ("[email protected]") Fri, 05 Sep 2025 17:16:22 +0000
Newsgroups php.notes
Message-ID <[email protected]>
The Enum documentation says, "if a Backed Enum is serialized to JSON, it will be represented by its scalar value only, in the appropriate type."

This means you can easily get a backed Enum's values for use in a JSON document using only the BackedEnum::cases() method:

<?php
enum Suits: string {
    case Hearts = 'Heart';
    case Diamonds = 'Diamond';
    case Clubs = 'Spade';
    case Spades = 'Club';
}
echo json_encode(Suits::cases());
?>

Results in this output:

["Heart","Diamond","Spade","Club"]
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 2604:3d09:567b:c531:4486:72e4:da03:5f30)
----
Manual Page -- https://php.net/manual/en/unitenum.cases.php
Edit        -- https://main.php.net/note/edit/130472
Del: integrated  -- https://main.php.net/note/delete/130472/integrated
Del: useless     -- https://main.php.net/note/delete/130472/useless
Del: bad code    -- https://main.php.net/note/delete/130472/bad+code
Del: spam        -- https://main.php.net/note/delete/130472/spam
Del: non-english -- https://main.php.net/note/delete/130472/non-english
Del: in docs     -- https://main.php.net/note/delete/130472/in+docs
Del: other reasons-- https://main.php.net/note/delete/130472
Reject      -- https://main.php.net/note/reject/130472
Search      -- https://main.php.net/manage/user-notes.php