Enum
Maginium introduces support for Enums, offering a clean and robust way to manage a predefined set of constants within your application. Enums are particularly useful for handling fixed values, like user roles, order statuses, or other categorical data. By leveraging Enums, you can improve code readability, reduce errors, and standardize the handling of constant data.
What are Enums?
Enums (short for Enumerations) are a special type of data structure that allows you to define a set of named constants. In Maginium, Enums are implemented as PHP backed Enums, providing both functionality and type safety.
For example, consider an Enum representing the roles in an application:
With the UserRole
Enum, you can easily reference roles in a type-safe manner:
Defining Enums in Maginium
To define an Enum in Maginium, use the enum
keyword followed by the Enum name and type. Enums can be backed by string
or int
values to associate constants with scalar data.
### Example: Order Status Enum
You can then use this Enum to manage order statuses consistently throughout your application:
Important: Enums provide a centralized source of truth for fixed constants, minimizing the risk of typos and logical errors.
Enum Methods
Enums in Maginium allow you to define methods within the Enum class to encapsulate logic specific to the Enum.
### Example: User Role Permissions
Using this method:
Adding methods to Enums promotes reusability and keeps related logic encapsulated within the Enum itself.
Enum Reflection
Maginium provides utility methods to work with Enums, including listing cases or retrieving values.
### Listing Enum Cases
### Validating Enum Values
Tip: Use reflection utilities to dynamically work with Enum cases and values without hardcoding logic.
Enums in Database
When using Enums in database operations, you can store the Enum values directly. Maginium simplifies this by seamlessly integrating Enums into Eloquent models.
### Example: Enum in Eloquent Models
With this setup, the status
field in the Order
model will automatically be cast to and from the OrderStatus
Enum.
Last updated