PHP Packagev8.5.xMIT License0+ downloads

Smart Enum: Native enums that know isActive()

Add use SmartEnum; to any native PHP enum and every case can answer for itself: $status->isActive() for Active, $order->isInProgress() for IN_PROGRESS. You also get helpers for names, values, labels, options, lookup by case name and strict comparisons. Your enums stay native: no base class, no code generation, no framework.

$composer require pharaonic/php-smart-enum
Key Features

Everything your enums are missing

One trait, added to the enums you already have.

isActive() for Every Case

Each case gets its own check, named from the case: isActive() for Active, isInProgress() for IN_PROGRESS, isOnHold() for onHold.

Names, Values & Labels

names(), values() and labels() list your cases in the shape you need.

Select Options

options() maps each backing value to its label, ready for a dropdown.

Lookup by Case Name

fromName(), tryFromName() and hasName() work with case names, the way from() works with values.

Strict Comparisons

eq(), is(), isNot(), in() and notIn() match a case, a case name or a backing value, with no type juggling.

Unit & Backed Enums

The same trait works on unit, string-backed and int-backed enums, and custom labels flow into labels() and options().

A quick look

Add the trait and every case answers its own is…() check, whatever style its name is written in.

app/Enums/OrderStatus.php
1use Pharaonic\SmartEnum\SmartEnum;
2 
3enum OrderStatus: string
4{
5 use SmartEnum;
6 
7 case PENDING = 'pending';
8 case IN_PROGRESS = 'in_progress';
9 case OUT_FOR_DELIVERY = 'out_for_delivery';
10 case Delivered = 'delivered';
11}
12 
13$status = OrderStatus::IN_PROGRESS;
14 
15$status->isInProgress(); // true
16$status->isOutForDelivery(); // false
17$status->in(['pending', 'in_progress']); // true
FAQ

Smart Enum Questions

Quick answers about installing and using Smart Enum.

What is Smart Enum?

One trait that adds framework-independent helpers to native PHP enums, such as names, values, labels, options, lookup by case name, strict comparisons and isActive() style case checks, without replacing cases(), from() or tryFrom(). It's a free, open-source PHP package by Pharaonic.

How do I install Smart Enum?

Run composer require pharaonic/php-smart-enum in your project's root directory.

What does Smart Enum require?

The latest release requires PHP 8.5+.

Does it replace cases(), from() or tryFrom()?

No. Those methods belong to PHP and stay untouched. Smart Enum only adds new helpers next to them.

Does it work with unit (non-backed) enums?

Yes. A unit enum has no backing values, so values(), options(), hasValue() and info() use the case names instead.

How do I customize labels?

Declare a label() method in the enum, or define a global smart_enum_label() function for every enum at once. labels(), options() and info() pick the change up.

How does IN_PROGRESS become isInProgress()?

The check is is plus the case name, with each underscore-separated word capitalized. A word written all in upper case is title-cased first, so IN_PROGRESS becomes isInProgress(), PENDING becomes isPending() and onHold becomes isOnHold().

Will my IDE know about $status->isActive()?

The is<Case>() checks are resolved at runtime, so add @method bool isActive() tags to the enum's docblock for IDEs and PHPStan.

Does it work with Laravel?

Yes. It's plain PHP, so it works in any framework, and it has no Laravel-specific code.

Is Smart Enum free to use?

Yes. Smart Enum is open source under the MIT license, so you can use it in personal and commercial projects.

Where can I find the Smart Enum documentation?

Read the Smart Enum documentation for setup, usage, and the full API reference.

How do I report a bug or contribute to Smart Enum?

Open an issue or a pull request on GitHub, or ask in the Pharaonic Discord.

Ready to dig in?

Read the full documentation for installation, the static and instance helpers, case checks, custom labels, unit vs backed enums, the API reference, and examples.