Skip to content

Supercharge Your PHP Enums with cerbero90/enum

Enums in PHP are like the unsung heroes of your code—they quietly make your life easier, offering a clean way to define a fixed set of values. Introduced in PHP 8.1, native enums already streamlined a lot of workflows, but you know how it goes: there’s always room for improvement. That’s where the cerbero90/enum package comes in. Think of it as PHP enums on steroids—more powerful, more flexible, and surprisingly easy to use.

Why Bother with Enums Anyway?

Before we dive into the good stuff, let’s answer the obvious question: why should you care about enums? Well, imagine managing user roles across an app. You could use strings like ‘admin’, ‘editor’, ‘user’ scattered everywhere, but that’s a recipe for typos and bugs. Enums give you a type-safe way to handle these values. Clean, consistent, and no more mysterious bugs from misspelled roles. Nice, right?

Enter cerbero90/enum: The Power Boost You Didn’t Know You Needed

The cerbero90/enum package takes PHP enums and cranks them up a notch. It adds methods and features that make working with enums more intuitive and, dare I say, enjoyable.

First things first, installation is a breeze:

composer require cerbero/enum

No fuss, no mess.

Real-World Example: Handling Order Statuses

Let’s say you’re building an e-commerce app. Orders can be ‘Pending’, ‘Shipped’, ‘Delivered’, or ‘Cancelled’. With cerbero90/enum, defining this looks clean and readable:

use Cerbero\Enum\Concerns\Enumerates;

enum OrderStatus
{
    use Enumerates;

    case Pending;
    case Shipped;
    case Delivered;
    case Cancelled;
}

Looks familiar, right? But here’s where it gets interesting.

Checking If a Status Exists

Want to check if a status exists? Simple:

OrderStatus::has('Pending'); // true
OrderStatus::has('Refunded'); // false

No more clunky in_array() checks or custom validation logic.

Comparing Values Like a Pro

Let’s say you’re processing orders and need to check statuses. Here’s how effortless it is:

$status = OrderStatus::Shipped;

if ($status->is('Shipped')) {
    // Notify customer
}

if ($status->isNot('Cancelled')) {
    // Proceed with delivery
}

Feels almost too easy, doesn’t it?

Add Context with Metadata

Sometimes a simple label doesn’t cut it. Maybe you want to attach descriptions or codes. cerbero90/enum has you covered with the Meta attribute.

use Cerbero\Enum\Attributes\Meta;

enum OrderStatus
{
    use Enumerates;

    #[Meta(description: 'Order is waiting to be processed')]
    case Pending;

    #[Meta(description: 'Order has been shipped to customer')]
    case Shipped;

    #[Meta(description: 'Order delivered successfully')]
    case Delivered;

    #[Meta(description: 'Order was cancelled by user or admin')]
    case Cancelled;
}

Retrieving that metadata? Easy:

echo OrderStatus::Pending->description(); // 'Order is waiting to be processed'

Filter and Retrieve Like a Boss

Need to work with specific sets of statuses? You can filter them effortlessly:

$activeStatuses = OrderStatus::except('Cancelled');
$shippingStatuses = OrderStatus::only('Pending', 'Shipped');

Suddenly, your code is more readable and maintainable.

Magic Methods—Because Why Not?

This package even throws in some magic methods. Want to get the value of a case?

OrderStatus::Pending(); // 'Pending'

It’s the small conveniences that make a big difference.

So, Is cerbero90/enum Worth It?

Honestly? If you’re serious about clean, maintainable PHP code, it’s a no-brainer. This package takes enums from good to great. Whether you’re managing order statuses, user roles, or any fixed set of values, cerbero90/enum saves time and headaches.

Why settle for basic when you can build smarter? Give it a shot, and your future self will thank you.

Ready to level up your enums?

Happy coding!

Published inPHP

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *