value-objects
Skill4 starsChanged 4 months agoMITView on GitHub
Model immutable domain primitives (Money, Email, Coordinate) as small value objects — constructor validation, invariants, equality by value, and readonly properties to avoid primitive obsession. Use when the user wraps a primitive (money, email, coordinate) in an immutable type with invariants; the user needs two domain values to be considered equal when their contents match, not their identity; the user passes bare strings or ints for domain concepts and wants a dedicated type to stop mix-ups. Keywords: value-object, ddd, immutability, readonly, equality, validation.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- TheBeardedBearSAS/claude-crafttheir own version, first seen Oct 29, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- TheBeardedBearSAS/claude-craft★ 108same 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
4 rules. About 0 are common, 3 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 67
Do not model something with a distinct identity and mutable lifecycle (a User, an Order) as a value object.
- Distinctive: line 68
Do not build a value object when the value is simply one of a small closed set of named options.
- Very specific: line 83
Add an equals(self $other): bool method comparing the fields, and with*() methods that return new instances for any change.
- Distinctive: line 92
Add static named constructors (e.g
≈ “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/value-objects/ --><!-- schema: atomVersion=3 skillVersion=2 graphVersion=1 --><!-- compile: @php-skills/compiler v1.0.0 (deterministic build) -->Value Objects
When to use
- The user wraps a primitive (money, email, coordinate) in an immutable type with invariants.
- The user needs two domain values to be considered equal when their contents match, not their identity.
- The user passes bare strings or ints for domain concepts and wants a dedicated type to stop mix-ups.
When NOT to use
- Do not model something with a distinct identity and mutable lifecycle (a User, an Order) as a value object.
- Do not build a value object when the value is simply one of a small closed set of named options.
Core guidance
- Use readonly properties for immutability — Declare the object's properties
readonly(PHP 8.1+) and set them only in the constructor, so the value cannot change after creation. - Enforce invariants in the constructor — Validate every invariant in the constructor and throw on violation, so an instance can never exist in an invalid state.
Quick recipe
- Declare the fields as promoted
readonlyconstructor parameters, e.g.public function __construct(public readonly int $amountCents, public readonly string $currency) {}. (verify: The property cannot be reassigned after construction) - In the constructor, check each invariant and throw
InvalidArgumentExceptionon violation (e.g. reject a negative amount or an empty currency code). (verify: Constructing with invalid data throws, never returns a bad instance) - Add an
equals(self $other): boolmethod comparing the fields, andwith*()methods that return new instances for any change. (verify: Two objects with equal fields report equal;with*()leaves the original unchanged)
Best practices
- Expose an
equals(self $other): boolmethod that compares the meaningful fields, rather than relying on===identity comparison - Scope each value object to one concept and give it the behavior that belongs to that concept (formatting, arithmetic, comparison) instead of leaking logic to callers
- Add static named constructors (e.g
- Model changes as
with*()methods that return a new instance with the changed field, never as setters that mutate the current object
Anti-patterns
| Anti-pattern | Why it fails | Do instead |
| Anemic wrapper that just holds a scalar | A pass-through wrapper adds ceremony without the benefits, and callers still work with the raw scalar. | Give the value object real invariants and the behavior for its concept, or drop the<br>wrapper if the raw scalar is genuinely sufficient. |
| Mutable value object with setters | Mutation breaks the core guarantee — a value shared across the app can change under callers' feet. | Make properties readonly and model changes as with*() methods that return a new<br>instance. |
| Validating outside the value object | Callers forget to validate, so invalid instances slip in and the type stops guaranteeing anything. | Move all invariant checks into the constructor (or a named constructor) so an instance<br>cannot be created in an invalid state. |
See references/anti-patterns.md.
Common mistakes
- Storing money as a float — Backing a Money value object with a float leads to rounding errors in arithmetic and comparison. Fix: Store money as integer minor units (cents) plus a currency, and do arithmetic on the integer.
- Using === to compare value objects — Comparing two value objects with
===tests object identity, so equal contents can compare as not equal. Fix: Add and use anequals(self $other): boolmethod (or==on identical shapes) to compare by value. - Mutating a readonly property when cloning — Assigning to a
readonlyproperty outside its declaring scope — including inside__clone()on PHP 8.1/8.2 — throws anError. Fix: Build changed copies withwith*()methods that construct a fresh instance; on PHP 8.3+, reinitialize readonly properties inside__clone()if you need clone-based copies.
Version notes
- Readonly properties added in PHP 8.1 (PHP >=8.1) —
readonlyproperties, which value objects use for immutability, are available from PHP 8.1; combine them with constructor property promotion for concise definitions. - Readonly classes added in PHP 8.2 (PHP >=8.2) — From PHP 8.2 a whole class can be marked
readonly, making every property readonly automatically — a natural fit for value objects. - Reinitializing readonly properties on clone in PHP 8.3 (PHP >=8.3) — From PHP 8.3,
__clone()may reinitializereadonlyproperties, enabling clone-basedwith*()copies that were impossible on 8.1 and 8.2.
Security
- Let the value object reject untrusted input — Construct value objects from untrusted input at the trust boundary so their constructor rejects malformed data (bad email, out-of-range coordinate) before it reaches the domain.
Testing
- Activation and decision behavior are verified in
eval/scenarios/php/value-objects.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
Related: ai-rules/strategy-readonly-immutability, architecture/hexagonal, architecture/repository-pattern, php/enums, php/readonly-properties, psr/psr7-http-messages
<!-- graph-fragment: graph/fragments/php/value-objects.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.

