Download the PHP package mimosafa/php-enumeration without Composer
On this page you can find all versions of the php package mimosafa/php-enumeration. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mimosafa/php-enumeration
More information about mimosafa/php-enumeration
Files in mimosafa/php-enumeration
Package php-enumeration
Short Description A powerful PHP 8.1+ library to create inheritable, feature-rich enumerations that extend beyond native enum capabilities.
License MIT
Informations about the package php-enumeration
PHP Enumeration
日本語のREADMEはこちら
A powerful PHP 8.1+ library to create inheritable, feature-rich enumerations that extend beyond native enum capabilities.
Why PHP Enumeration?
PHP 8.1 introduced native enumerations, which are a great addition to the language. However, they come with a few limitations:
- They cannot be extended (no
extends). - They are strictly typed as
stringorintfor backed enums.
This package provides a set of base classes and traits to create enumerations that overcome these limitations, offering a more flexible and powerful way to work with enums in your PHP 8.1+ projects.
Key features include:
- Inheritance: Extend your enums to create more complex and reusable structures.
- Convenient Factory Methods: Get enum instances by case name using
::of()and::tryOf(). - Flexible Backed Values: Use any scalar value for your backed enums, not just
stringorint. - Dynamic Case Control: Subclasses can precisely control which cases to inherit from a parent class.
Comparison with Native PHP Enums
While this library offers powerful features, it's important to understand the differences and choose the right tool for your needs.
| Feature / Aspect | Native PHP Enums (PHP 8.1+) | This Library (mimosafa/php-enumeration) |
|---|---|---|
| Inheritance | ❌ Not supported | ✅ Supported |
| Flexible Backed Values | ❌ string or int only |
✅ Any scalar (string, int, float, bool) |
| Instance lookup by name | ✅ (Direct static access) | ✅ Supported (via ::of() / ::tryOf()) |
| Dynamic Case Definition | ❌ Not supported | ✅ Supported (via toArray() or EnumerateConstantsTrait) |
match Expression |
✅ Supported | ❌ Not directly supported |
| Type Hinting | enum keyword |
Class name (e.g., PureEnum, BackedEnum) |
Built-in cases() |
✅ Supported | ✅ Supported (via custom implementation) |
Choose this library if you need advanced features like inheritance or flexible backed values. For simpler use cases where match expression support and strict string/int backed values are preferred, native PHP Enums might be sufficient.
Installation
You can install the package via composer:
Usage
Pure Enums
Create a simple enum without backed values. The toArray() method allows for dynamic definition of cases.
If you want IDE autocompletion for magic static calls like Status::PUBLISHED(), add @method annotations to the child class PHPDoc as shown below.
Dynamic Case Definition Example:
You can define enum cases dynamically, for instance, by reading file names from a directory:
Backed Enums
Create enums with scalar values.
If you want IDE autocompletion for magic static calls like Suit::Diamonds(), add @method annotations to the child class PHPDoc as shown below.
Inheritance with EnumerateConstantsTrait
This is where the magic happens. Define your cases as class constants and use inheritance to build powerful, domain-specific enums.
The EnumerateConstantsTrait automatically turns your class constants into enum cases.
1. Define a base enum:
2. Extend and control the cases:
Now, you can create specialized enums that inherit from UserRole but only expose a subset of the cases.
You can also configure this declaratively with PHP attributes (useful for IDE discoverability):
Available attributes:
#[IncludeConstants('A', 'B')]#[ExcludeConstants('A', 'B')]#[AllowDuplicateValues(false)]#[DisableCaseNameStaticCall](forPureEnumsubclasses)
3. Use them in your application:
License
The MIT License (MIT). Please see License File for more information.