Download the PHP package php-compatible/enum without Composer
On this page you can find all versions of the php package php-compatible/enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package enum
Enum
PHP 8-style enums for PHP 7.2+ - Write enums today, upgrade to native enums tomorrow.
This library provides a PHP 8-compatible enum API that works on PHP 7.2 and above. When you're ready to upgrade to PHP 8.1+, use the included migration tool to automatically convert your enums to native PHP syntax.
Why Use This Package?
- Future-proof: API mirrors PHP 8 enums (
from(),tryFrom(),cases()) - Zero-friction migration: Automated upgrade tool converts to native PHP 8 enums
- IDE-friendly: CLI tool auto-generates PHPDoc annotations for full autocompletion
- Flexible: Case-insensitive access, auto-increment values, string or int backing
- Human-readable labels: Built-in conversion from
camelCasetoCamel Case - Type-safe: Strict type comparison for value lookups (matches PHP 8 behavior)
- Well-tested: 100% code coverage across PHP 7.2 - 8.5
CLI Tools Included
| Command | Description |
|---|---|
vendor/bin/php-compatible-enum-auto-doc |
Auto-generate @method PHPDoc annotations for IDE support |
vendor/bin/php-compatible-enum-upgrade-to-php8 |
Migrate enums to native PHP 8.1+ syntax |
Installation
Documentation
Full documentation available at phpcompatible.dev/docs/category/enum
Usage
Basic Enum (Auto-increment)
Define enum cases as protected properties. Uninitialized properties auto-increment from 0:
Mixed Values
Combine auto-increment with explicit values:
String Enum
Case-Insensitive Access
Access enum cases using any naming convention:
Getting All Cases
Looking Up by Value
Use from() and tryFrom() to get an enum case from its backing value (PHP 8 compatible):
Duplicate backing values are not allowed (matches PHP 8 behavior):
Human-Readable Labels
Use EnumLabel to convert enum names to human-readable labels:
EnumLabel handles various naming conventions:
| Input | Output |
|---|---|
camelCase |
Camel Case |
PascalCase |
Pascal Case |
snake_case |
Snake Case |
SCREAMING_SNAKE |
Screaming Snake |
ABCValue |
ABC Value |
Labels auto-convert to strings:
IDE Support
Add PHPDoc @method annotations for autocompletion:
Auto-generating Annotations
Use the php-compatible-enum-auto-doc CLI tool to automatically generate @method annotations:
The tool scans PHP files for classes that use PhpCompatible\Enum\Enum, extracts protected properties, and updates the class docblock with appropriate @method annotations. Files are listed as they are updated.
Upgrading to PHP 8 Native Enums
When you're ready to migrate to PHP 8.1+ native enums, use the php-compatible-enum-upgrade-to-php8 CLI tool:
The tool will:
- Convert enum class definitions - Transform
class Status extends Enumtoenum Status - Update case declarations - Convert
protected $draft;tocase draft; - Handle backed enums - Preserve integer or string backing values
- Update usages - Replace
Status::draft()withStatus::draft(removes parentheses) - Swap EnumLabel - Replace
EnumLabel::from()withPhp8EnumLabel::from()
Before:
After:
How It Works
- Enum cases are defined as
protectedinstance properties - Properties are immutable from outside the class
__callStaticenables static-style access:Suit::hearts()- A singleton instance is created internally for reflection
- Values are lazily loaded and cached on first access
null(uninitialized) values auto-increment from 0- Case-insensitive matching allows flexible access styles
API Reference
Enum
| Method | Returns | Description |
|---|---|---|
CaseName() |
Value |
Get enum case (case-insensitive) |
cases() |
Value[] |
Get all enum cases |
from($value) |
Value |
Get case by value (throws if not found) |
tryFrom($value) |
Value\|null |
Get case by value (null if not found) |
Value
| Property | Type | Description |
|---|---|---|
$name |
string |
The enum case name (original definition) |
$value |
int\|string\|null |
The enum case value |
EnumLabel
| Method | Returns | Description |
|---|---|---|
from(Value $value) |
EnumLabel |
Create label from enum value |
toString() |
string |
Get label as string |
__toString() |
string |
Auto string conversion |
Php8EnumLabel
For PHP 8.1+ native enums (extends EnumLabel):
| Method | Returns | Description |
|---|---|---|
fromEnum(UnitEnum $case) |
Php8EnumLabel |
Create label from PHP 8 enum case |
toString() |
string |
Get label as string |
__toString() |
string |
Auto string conversion |
Requirements
- PHP 7.2 or higher
License
MIT