enums
Skill4 starsChanged 4 months agoMITView on GitHub
Model fixed sets of named values with PHP 8.1+ enumerations — backed and pure enums, safe input validation, methods, and interfaces. Use when the user needs a fixed, closed set of named values such as order status, role, or state; the user wants to replace magic strings or class constants with a type-safe enum on PHP 8.1+; the user maps untrusted HTTP or API input to a known enum set and must reject invalid values. Keywords: enums, php81, type-safety, backed-enum, validation.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- idavidov13/agentic-playwrighttheir own version, first seen Nov 8, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- idavidov13/agentic-playwright★ 268same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository119 other files it gives its agents.
- strategy-bitrix-d7-incrementalSkill
- strategy-explicit-over-magicSkill
- strategy-framework-conventionsSkill
- strategy-legacy-safe-editsSkill
- strategy-minimal-diffSkill
- strategy-no-untested-refactorSkill
- strategy-preserve-public-apiSkill
- strategy-readonly-immutabilitySkill
- strategy-security-firstSkill
- strategy-test-drivenSkill
- openapiSkill
- paginationSkill
- rest-designSkill
- versioningSkill
- cqrsSkill
- dependency-inversionSkill
- hexagonalSkill
- repository-patternSkill
- service-layerSkill
- bitrixSkill
- findings-reportSkill
- laravelSkill
- performanceSkill
- phpSkill
- project-analysisSkill
- refactoringSkill
- roi-prioritizationSkill
- securitySkill
- stack-detectionSkill
- cache-layersSkill
- component-engineSkill
- d7-eventsSkill
- d7-loaderSkill
- d7-orm-queriesSkill
- iblock-elementsSkill
- kernel-main-moduleSkill
- localizationSkill
- controllersSkill
- ormSkill
- validationSkill
- controllersSkill
- query-builderSkill
- routingSkill
- autoload-psr4Skill
- dependency-managementSkill
- scriptsSkill
- connection-poolingSkill
- mysql-indexingSkill
- redisSkill
- transactionsSkill
- ci-github-actionsSkill
- deployment-strategiesSkill
- docker-phpSkill
- env-configSkill
- associationsSkill
- entities-mappingSkill
- orm-basicsSkill
- entity-apiSkill
- module-developmentSkill
- routing-controllersSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 66
Do not use this skill when the value set is open-ended or third parties must add values at runtime.
- Distinctive: line 67
Do not use enums when the target runtime is PHP 7.4 or 8.0.
- Very specific: line 80
Add a label(): string method using match ($this) to map each case to display text.
- Very specific: line 85
Use the generated Enum::cases() method to iterate all values (for dropdowns, validation lists, or seeding) instead of maintaining a …
- Very specific: line 89
Put value-specific behavior (labels, colors, transitions) in methods on the enum itself, typically with a match ($this) expression, rather …
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
<!-- generated: do not edit — source: knowledge/atoms/php/enums/ --><!-- schema: atomVersion=3 skillVersion=2 graphVersion=1 --><!-- compile: @php-skills/compiler v1.0.0 (deterministic build) -->PHP Enums
When to use
- The user needs a fixed, closed set of named values such as order status, role, or state.
- The user wants to replace magic strings or class constants with a type-safe enum on PHP 8.1+.
- The user maps untrusted HTTP or API input to a known enum set and must reject invalid values.
When NOT to use
- Do not use this skill when the value set is open-ended or third parties must add values at runtime.
- Do not use enums when the target runtime is PHP 7.4 or 8.0; use class constants with allow-list validation instead.
Core guidance
- Choose backed enums when values are persisted or transmitted — Use a backed enum (
enum Status: string) when values are stored in a database, serialized to JSON, or received from input. Use a pure enum when the values are purely in-memory tokens with no external representation. - Use tryFrom for untrusted input, from for trusted values — Call
tryFrom()on untrusted input and handle thenullresult; callfrom()only when the value originates from a trusted, already-validated source.
Quick recipe
- Declare
enum OrderStatus: stringand list each case with its stored scalar value. (verify:OrderStatus::Paid->value === 'paid') - Add a
label(): stringmethod usingmatch ($this)to map each case to display text. (verify:OrderStatus::Paid->label()returns the expected string) - At the controller boundary, resolve input with
OrderStatus::tryFrom($input)and return 422 on null. (verify: Invalid input yields a 422 response, not a 500)
Best practices
- Use the generated
Enum::cases()method to iterate all values (for dropdowns, validation lists, or seeding) instead of maintaining a separate array - Have enums implement an interface when several enums must be used interchangeably (e.g
- Put value-specific behavior (labels, colors, transitions) in methods on the enum
itself, typically with a
match ($this)expression, rather than in external arrays
Anti-patterns
| Anti-pattern | Why it fails | Do instead |
| Class constants where an enum fits | Class constants give no type safety — any string is accepted, and IDEs cannot enforce the set. | Use a backed enum so the type system rejects invalid values at the boundary. |
| Passing scalar around instead of the enum | Downgrading to a scalar discards type safety, re-introducing the stringly-typed bugs enums remove. | Keep the enum type across the domain; convert to ->value only at the persistence or serialization edge. |
| Comparing enum to raw string | An enum instance is never identical to a scalar, so the comparison is always false and the bug is silent. | Compare enum-to-enum ($status === Status::Paid) or compare $status->value === 'paid' when a scalar is unavoidable. |
See references/anti-patterns.md.
Common mistakes
- from() throws ValueError on unknown input — Calling
Enum::from($userInput)on an unknown value throws an uncaughtValueError. Fix: UsetryFrom()and handlenull, or wrapfrom()in a try/catch at trust boundaries. - Adding a case breaks non-exhaustive match — A
match ($enum)without adefaultthrowsUnhandledMatchErrorwhen a new case is added. Fix: Rely on static analysis (PHPStan level 8) to flag non-exhaustive matches at build time rather than adding a catch-all default that hides new cases. - Pure enums do not serialize to a scalar — A pure (non-backed) enum has no
->value, sojson_encode()emits an object, not a scalar. Fix: Use a backed enum when the value crosses a serialization boundary (JSON, DB, cache).
Version notes
- Enums introduced in PHP 8.1 (PHP >=8.1) — Enumerations (pure and backed),
cases(),from(), andtryFrom()are available from PHP 8.1. - Typed class constants complement enums in PHP 8.3 (PHP >=8.3) — From PHP 8.3, constants declared on an enum can be typed, tightening contracts shared via interfaces.
- PHP 8.5 enum refinements (PHP >=8.5) — PHP 8.5 refines enum constant handling; no code changes are required when migrating enum-heavy code from 8.4 to 8.5.
Security
- Validate enum input at the trust boundary — Treat any enum value sourced from HTTP, CLI, or third-party payloads as untrusted:
resolve it with
tryFrom()and rejectnullbefore it enters domain logic.
Testing
- Activation and decision behavior are verified in
eval/scenarios/php/enums.yaml.
Additional resources
- references/anti-patterns.md
- references/core.md
- references/examples.md
- references/pitfalls.md
- references/recipes.md
- references/version-differences.md
Knowledge graph
Prerequisites: php/typed-properties
Related: php/attributes, php/class-constants, php/match-expression, php/value-objects
<!-- graph-fragment: graph/fragments/php/enums.yaml -->Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

