enum flag backed enum
| From: | Rob Landers | Date: | Wed, 03 Sep 2025 20:44:58 +0000 |
| Subject: | enum flag backed enum | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-128616@lists.php.net to get a copy of this message | ||
Hello internals,
I would like to propose a new type of backed enum: flag. This is an integer-backed enum that
requires power-of-two values that can be combined with the bitwise operators to be used as flags.
In some core functions, it might look like this:
enum JSON_FLAG: flag {
case None = 0;
case ThrowOnError = 0x1 << 1;
// etc.
case PrettyPrint = 0x01 << 4;
// etc.
}
function json_decode(string $data, bool $assoc = false, int $depth = 512, JSON_FLAG|int $options =
JSON_FLAG::None) {}
json_decode($data, options: JSON_FLAG::ThrowOnError | JSON_FLAG::PrettyPrint);
Note that the goal here isn’t so much to save typing space, as it is to provide easier
discoverability through IDEs, better type safety, and better ergonomics in the language overall.
Attempting to use a backed value that is not a power-of-two would result in a compilation error.
Bitwise operations are performed as though they’re performed on a regular integer.
What do you think?
I couldn’t find any similar proposals (listed on the RFC page) before proposing this on the list,
so if you’re working on this, please speak up; I’d like to join forces.
— Rob