agentleFS
AnyOrgNameAnyOrgName/php-skills/skills/php/value-objects/SKILL.md

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

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
  1. Distinctive:

    Do not model something with a distinct identity and mutable lifecycle (a User, an Order) as a value object.

    line 67
  2. Distinctive:

    Do not build a value object when the value is simply one of a small closed set of named options.

    line 68
  3. Very specific:

    Add an equals(self $other): bool method comparing the fields, and with*() methods that return new instances for any change.

    line 83
  4. Distinctive:

    Add static named constructors (e.g

    line 92

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md
<!-- 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

  1. Declare the fields as promoted readonly constructor parameters, e.g. public function __construct(public readonly int $amountCents, public readonly string $currency) {}. (verify: The property cannot be reassigned after construction)
  2. In the constructor, check each invariant and throw InvalidArgumentException on violation (e.g. reject a negative amount or an empty currency code). (verify: Constructing with invalid data throws, never returns a bad instance)
  3. Add an equals(self $other): bool method comparing the fields, and with*() 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): bool method 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-patternWhy it failsDo instead
Anemic wrapper that just holds a scalarA 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 settersMutation 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 objectCallers 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

  1. 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.
  2. 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 an equals(self $other): bool method (or == on identical shapes) to compare by value.
  3. Mutating a readonly property when cloning — Assigning to a readonly property outside its declaring scope — including inside __clone() on PHP 8.1/8.2 — throws an Error. Fix: Build changed copies with with*() 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) — readonly properties, 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 reinitialize readonly properties, enabling clone-based with*() 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 -->

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.