← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Enums — First-Class Enumerations (PHP 8.1)

php PHP 8.1+ Intermediate

TL;DR

PHP 8.1 native enums replace class constant hacks — providing type-safe, enumerable, matchable values with optional backing values (: string or : int).

Explanation

Two enum types: pure (unit) enums: enum Status { case Active; case Inactive; } and backed enums: enum Status: string { case Active = 'active'; case Inactive = 'inactive'; }. Methods, interfaces, and constants in enums. Static methods. from() / tryFrom() for backed enums. cases() returns all cases. Enums extend UnitEnum (pure) or BackedEnum (backed). Cannot be instantiated with new. Cannot extend classes. Enum cases are singletons — Status::Active === Status::Active always. In match expressions: exhaustive matching. In type hints: function process(Status $status). Compare with class constants: no type safety, no first-class status.

Common Misconception

PHP enums are like Java/TypeScript enums which are just typed numbers — PHP enums are objects (singletons), can have methods, implement interfaces, and backed enums have actual string/int values.

Why It Matters

Enums eliminate the class-constant-as-enum pattern, providing type safety (Status not string), exhaustive matching in match expressions, and self-documenting code.

Common Mistakes

  • Comparing enum cases with == instead of === — always use ===.
  • Using string class constants as enum replacement — loses type safety.
  • Not using tryFrom() for user input — from() throws on invalid value.

Code Examples

✗ Vulnerable
// Class constants — no type safety:
class Status {
    const ACTIVE = 'active';
    const INACTIVE = 'inactive';
}
function setStatus(string $status) {} // Accepts any string
✓ Fixed
enum Status: string {
    case Active = 'active';
    case Inactive = 'inactive';
}

function setStatus(Status $status): void {}
setStatus(Status::Active); // Type-safe

// User input:
$status = Status::tryFrom($request->input('status')) ?? Status::Active;

Added 23 Mar 2026
Views 27
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 3 pings W 0 pings T 0 pings F 1 ping S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 2 pings S 0 pings M 0 pings T 0 pings W 2 pings T 0 pings F 1 ping S 0 pings S 0 pings M 2 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 6 Google 6 Unknown AI 5 ChatGPT 3 Perplexity 3 SEMrush 2 Ahrefs 1
crawler 23 crawler_json 1 pre-tracking 2
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Medium
⚡ Quick Fix
Replace class constant patterns with backed enums. Use Status::tryFrom() for user input. Use in match expressions for exhaustive handling.
📦 Applies To
PHP 8.1+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
class.*const [A-Z]
Auto-detectable: ✗ No phpstan rector
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Class Tests: Update

✓ schema.org compliant