Supercharge Your PHP Enums with archtechx/enums

Supercharge Your PHP Enums with archtechx/enums

Overview of PHP Enums and archtechx/enums

PHP 8.1 introduced native enumerations (enums), which define a type-safe set of named values for modeling fixed sets of constants, such as statuses or roles. Enums can be pure (no associated values) or backed (tied to string or integer values). The match expression, introduced in PHP 8.0, provides a concise, type-safe alternative to switch statements.

The archtechx/enums package extends native enums with traits that simplify common tasks, such as retrieving case names, values, or metadata, and making enums invokable. These features reduce boilerplate code and improve developer experience. 

Key Features of archtechx/enums:
  • InvokableCases: Allows invoking enum cases statically or on instance to get their values(backed enums) or names(pure enums) without ->value.
  • Names: Returns an array of enum case names.
  • Values: Returns an array of enum case values (backed enums) or names (pure enums).
  • Options: Returns an associative array of case names and values (backed enums) or names (pure enums).
  • From: Adds from() and tryFrom() for reverse lookup by name for pure and backed enums.

Installation

Install the package via Composer:
Installation 
No additional setup is required, as the package provides traits to include in your enums. It requires 
PHP 8.1+. 

Guide to PHP Enums with archtechx/enums Features

Below is a comprehensive guide to using PHP enums, the match expression, and the archtechx/enums package, with examples demonstrating each trait.

1. Basic Enum Usage with match:

First, let’s review a basic enum with a match expression, then enhance it with archtechx/enums traits.
Pure Enum Example: 
Basic Enum Usage with match 
Backed Enum Example: 
Backed Enum Example 

2. Enhancing Enums with archtechx/enums Traits:

The archtechx/enums package provides traits to simplify enum usage. Below are examples of 
each trait, integrated into a practical order status system.
 
Trait 1: InvokableCases 
The InvokableCases trait allows you to invoke enum cases statically (e.g., Status::Pending()
or on instances (e.g., $status()) to get the backed value (for backed enums) or the case name 
(for pure enums), eliminating the need to use ->value.
Example: InvokableCases
InvokableCases
Benefit: Simplifies accessing enum values without ->value, improving readability 
and IDE support (autosuggestions for cases).
 
Trait 2: Names
The Names trait adds a names() method to return an array of enum case names.
Example: Names
Trait 2: Names   
Benefit: Easily retrieve case names for display in dropdowns, logs, or debugging.

Trait 3: Values
The Values trait adds a values() method to return an array of backed values (for backed enums) or 
case names (for pure enums).
Example: Values
Trait 3: Values
Benefit: Useful for retrieving values to store in databases or use in APIs.
 
Trait 4: Options
The Options trait adds an options() method to return an associative array of case names and values 
(for backed enums) or names (for pure enums).
Example: Options
Trait 4: Options
Benefit: Ideal for generating key-value pairs for form select inputs or configuration arrays.
 
use stringOptions():
The trait also adds the stringOptions() method that can be used for generating convenient 
string representations of your enum options:
use stringOptions(): 
For pure enums (non-backed), the name is used in place of $value (meaning that both $name and 
$value are the same).
 
Both arguments for this method are optional, the glue defaults to \n and the callback defaults to 
generating HTML <option> tags:
HTML tags
The From trait adds fromName(), tryFromName(), from(), and tryFrom() methods to convert 
names or values to enum cases, extending PHP’s native from() and tryFrom() for both pure and 
backed enums.
Example: From
From trait  
Benefit: Simplifies reverse lookups, especially for pure enums, and provides safe handling of invalid inputs.

Best Practices with archtechx/enums

  • Use InvokableCases for Simplicity: Avoid ->value in database or API interactions by invoking enums directly.
  • Leverage Options for Forms: Use options() to populate select inputs in forms.
  • Combine with match: Use match for clean, type-safe logic with enum cases.
  • Handle Invalid Inputs: Use tryFrom() or tryFromName() to gracefully handle invalid values.
  • Organize Enums: Place enums in an app/Enums directory for consistency in Laravel projects. 
This guide demonstrates how the archtechx/enums package enhances PHP enums with practical, real-world examples. For more details, check the package’s GitHub repository: https://github.com/archtechx/enums If you need further examples or specific use cases, let me know!
 
If you found this helpful, feel free to share or drop a comment. Happy coding with Laravel! 🧱✨ 
 
Read More Article