Supercharge Your PHP Enums with archtechx/enums
July 1, 2025Supercharge Your PHP Enums with archtechx/enums
PHP 8.1 introduced native enums—type‑safe sets of named values like statuses or roles. The archtechx/enums
package adds traits that reduce boilerplate and enhance DX (Developer Experience).
✨ Key Features
- InvokableCases: Call enum cases directly to get value or name, no more
->value
. - Names: Get an array of case names.
- Values: Fetch values (backed enums) or names (pure enums).
- Options: Associative array of case => value.
- From: Adds
from()
,tryFrom()
,fromName()
,tryFromName()
. - Metadata: Associate attributes like description/color per case.
- Comparable: Compare enums with
is()
,isNot()
,in()
,notIn()
.
📦 Installation
Requires PHP 8.1+
. Install via Composer:
composer require archtechx/enums
🧩 Examples
1. InvokableCases
use ArchTech\Enums\InvokableCases;
enum TaskStatus: int {
use InvokableCases;
case INCOMPLETE = 0;
case COMPLETED = 1;
case CANCELED = 2;
}
TaskStatus::COMPLETED(); // ⇒ 1
2. Names & Values
use ArchTech\Enums\Names;
use ArchTech\Enums\Values;
enum Role {
use Names, Values;
case ADMIN, USER, GUEST;
}
Role::names(); // ⇒ ['ADMIN', 'USER', 'GUEST']
Role::values(); // ⇒ ['ADMIN', 'USER', 'GUEST']
3. Options & stringOptions()
use ArchTech\Enums\Options;
enum TaskStatus: int {
use Options;
case INCOMPLETE = 0;
case COMPLETED = 1;
}
TaskStatus::options();
// ⇒ ['INCOMPLETE' => 0, 'COMPLETED' => 1]
TaskStatus::stringOptions();
// ⇒ "Incomplete…"
4. From / tryFrom / fromName / tryFromName
use ArchTech\Enums\From;
enum Role: string {
use From;
case ADMIN = 'admin';
case USER = 'user';
}
Role::from('admin'); // ⇒ Role::ADMIN
Role::tryFrom('unknown'); // ⇒ null
Role::fromName('USER'); // ⇒ Role::USER
5. Metadata
use ArchTech\Enums\Metadata;
use ArchTech\Enums\Meta\Meta;
use App\Enums\Meta\{Description, Color};
#[Meta(Description::class, Color::class)]
enum TaskStatus: int {
use Metadata;
#[Description('Incomplete')] #[Color('red')]
case INCOMPLETE = 0;
}
TaskStatus::INCOMPLETE->description(); // ⇒ 'Incomplete'
6. Comparable
use ArchTech\Enums\Comparable;
enum TaskStatus: int {
use Comparable;
case INCOMPLETE = 0;
case COMPLETED = 1;
}
TaskStatus::INCOMPLETE->is(TaskStatus::INCOMPLETE); // ⇒ true
✅ Best Practices
- Use
InvokableCases
instead of->value
for readability and IDE support. - Populate form selects with
options()
. - Use
tryFrom()
for safe input handling. - Group enums in an
app/Enums
directory in Laravel projects.
This package makes PHP enums way more powerful and developer-friendly—perfect for Laravel, APIs, forms, and metadata‑rich logic.
Blog
Mastering Modern CSS: The Power of if(), Popover Hints, and Smart Styling
Jul 16, 2025
🌐 Mastering Modern CSS: The Power of if(), Popover Hints, and Smart Styling CSS is getting smarter. In this guide, we’ll explore how the new...
React Native 0.80 & ExecuTorch: A Powerful Leap into Offline AI for Mobile Apps
Jul 28, 2025
🚀 What’s New in React Native 0.80? The React Native 0.80 release marks a pivotal moment in mobile development. This update not only enhances...
Efficient Reporting & Big Data Reports with Queues
Jul 07, 2025
How to cache report queries with fixed timelines How to generate large reports asynchronously using job queues 1. 🧠 Report Query Caching wi...
Task Reminder with Laravel & MongoDB
Jun 30, 2025
📌 Building a Task Reminder App This guide shows how to set up a Laravel app using MongoDB to implement a task reminder system with authentication,...
Laravel Queue & Job System: From Table Creation to Production Deployment
Jul 01, 2025
🚀 Laravel Queue & Job System We’re gonna walk you through Laravel queues from setup to deploying in production using Supervisor. Step 1...
Stop Copy-Pasting Code! Learn How to Use Traits in Laravel the Right Way
Jul 01, 2025
🚫 Stop Copy-Pasting Code! Ever duplicated slug logic or logging across multiple models? Laravel's Traits got your back. 1. What’s a Trait?...
