Quickstart
This walkthrough shows the smallest practical path from installation to a useful enum. It intentionally covers the three ideas new users need first:
- adding methods directly to an enum with traits
- reading the same data externally with
EnumHelper - understanding why both approaches exist
Build a Backed Enum with Trait Helpers
Start with a backed enum because it demonstrates the most common helpers in one place.
<?php
declare(strict_types=1);
use FastForward\Enum\Trait\HasNameLookup;
use FastForward\Enum\Trait\HasNames;
use FastForward\Enum\Trait\HasOptions;
use FastForward\Enum\Trait\HasValues;
enum Status: string
{
use HasNameLookup;
use HasNames;
use HasOptions;
use HasValues;
case Draft = 'draft';
case Published = 'published';
}
Status::values(); // ['draft', 'published']
Status::names(); // ['Draft', 'Published']
Status::options(); // ['Draft' => 'draft', 'Published' => 'published']
Status::fromName('Draft'); // Status::Draft
What this gives you
values()returns the backed values in declaration ordernames()returns case names without requiring manualcases()mappingoptions()returns a simplename => valuearray for forms, prompts, or docsfromName()adds a native-feeling name lookup that PHP does not provide out of the box
Reading the Same Enum Through EnumHelper
<?php
declare(strict_types=1);
use FastForward\Enum\Helper\EnumHelper;
EnumHelper::names(Status::class); // ['Draft', 'Published']
EnumHelper::valueMap(Status::class); // ['draft' => Status::Draft, 'published' => Status::Published]
This is useful when:
- you cannot or do not want to modify the enum declaration
- you want to keep utility logic in an application service
- the enum comes from another package
Seeing a Packaged Enum
<?php
declare(strict_types=1);
use FastForward\Enum\Calendar\Month;
use FastForward\Enum\Logger\LogLevel;
use FastForward\Enum\Runtime\Environment;
use FastForward\Enum\Sort\SortDirection;
Environment::Production->isProduction(); // true
Month::December->quarter(); // 4
LogLevel::Critical->isAtLeast(LogLevel::Warning); // true
SortDirection::Descending->reverse(); // SortDirection::Ascending
Choosing the Right Surface
| Need | Use | Why |
|---|---|---|
| Add methods to your own enum | Traits | The enum advertises the API directly, such as Status::values()
. |
| Inspect an enum you do not own | EnumHelper
|
Helpers work with class strings and enum cases without changing the enum declaration. |
| Reuse a general concept | Packaged enums | Shared enums provide stable semantics for calendars, sorting, runtime environments, logging levels, and similar concerns. |
What to read next
- Continue with Helper Methods if you want a method-by-method guide for traits and
EnumHelper. - Continue with Working with Enums if you want to see where the package sits inside application code.
- Continue with Enum Catalogs if you want to decide whether a packaged enum fits your use case.