An enumeration (Enum) in PHP is a restricted, custom data type that encapsulates a fixed, limited set of possible values. Introduced in PHP 8.1, enums are built on top of the class and object engine. Each enum case is a singleton instance of the enum itself, meaning they are passed by value (where the value is an object identifier/handle) and pass strict identity checks (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
===).
Unit Enums
A Basic or “Unit” Enum contains cases that have no scalar equivalent. They are identified solely by their name.Backed Enums
A Backed Enum assigns a scalar value (int or string) to each case. The backing type must be explicitly declared in the enum definition, and all cases must be of that specific type.
Built-in Properties and Methods
Enums provide several native properties and methods for introspection and instantiation:name: A read-only property available on all enums that returns the string name of the case.value: A read-only property available only on Backed Enums that returns the assigned scalar value.::cases(): A static method that returns a packed array of all defined cases in declaration order (the exact order they are defined in the source code).::from(int|string $value): A static method (Backed Enums only) that returns the enum instance corresponding to the scalar value. Throws aValueErrorif the value does not exist.::tryFrom(int|string $value): A static method (Backed Enums only) that returns the enum instance, ornullif the scalar value is not found.
Constants, Methods, Interfaces, and Traits
Enums share a class-like architecture, allowing them to declare their own class constants, methods, implement interfaces, and use traits. When using traits, a critical restriction applies: traits used by enums must not declare any properties, because enums are strictly stateless. Attempting to use a trait with properties will result in a fatal error. Within an enum method,self and static refer to the enum type, while $this refers to the specific case instance.
Technical Restrictions
Because enums are designed to represent immutable, stateless values, the PHP engine enforces several strict limitations:- No Instantiation: Enums cannot be instantiated using the
newkeyword. - No State: Enums cannot declare properties.
- No Inheritance: Enums cannot
extendclasses or other enums, and cannot be extended. They are implicitlyfinal. - Restricted Magic Methods: Magic methods related to state or instantiation (like
__get,__set,__clone,__sleep,__wakeup) are forbidden. Furthermore, enums are strictly forbidden from implementing__toString()or theStringableinterface. They are permitted to implement__call,__callStatic,__invoke, and__debugInfo. - Literal Expressions: Backed enum values must be strictly literal expressions (e.g.,
200or'admin'). Variables, constants, and constant expressions (such as1 + 1orSOME_CONSTANT) are entirely unsupported for enum case values.
Master PHP with Deep Grasping Methodology!Learn More





