PHPackages                             tiny-blocks/value-object - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. tiny-blocks/value-object

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

tiny-blocks/value-object
========================

Defines the default behavior contract for PHP value objects with structural equality.

5.0.1(1mo ago)323.7k↓79%3MITPHPPHP ^8.5CI passing

Since Jul 26Pushed 6d agoCompare

[ Source](https://github.com/tiny-blocks/value-object)[ Packagist](https://packagist.org/packages/tiny-blocks/value-object)[ Docs](https://github.com/tiny-blocks/value-object)[ RSS](/packages/tiny-blocks-value-object/feed)WikiDiscussions main Synced yesterday

READMEChangelog (10)Dependencies (15)Versions (14)Used By (3)

Value Object
============

[](#value-object)

[![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](LICENSE)

- [Overview](#overview)
- [Installation](#installation)
- [How to use](#how-to-use)
    - [Declaring a Value Object](#declaring-a-value-object)
    - [Comparing with equals](#comparing-with-equals)
    - [Hashing with hashCode](#hashing-with-hashcode)
    - [Behavior with nested types](#behavior-with-nested-types)
- [FAQ](#faq)
- [License](#license)
- [Contributing](#contributing)

Overview
--------

[](#overview)

A **V**alue **O**bject (**VO**) is a type that is distinguishable only by the state of its properties. Unlike an entity, which has a unique identifier and remains distinct even if its properties are identical, VOs with the same properties are considered equal.

This library exposes the `ValueObject` interface, which defines structural equality through `equals()` and a deterministic hash through `hashCode()`, and the `ValueObjectBehavior` trait, which provides the default recursive implementation for both operations.

More details about [VOs](https://martinfowler.com/bliki/ValueObject.html).

Installation
------------

[](#installation)

```
composer require tiny-blocks/value-object
```

How to use
----------

[](#how-to-use)

### Declaring a Value Object

[](#declaring-a-value-object)

Declare your VO as `readonly class` (PHP 8.2+) so that PHP enforces immutability at the language level. Mutable properties violate Value Object semantics even when `equals` and `hashCode` still function.

Implementations must declare properties as `public`. Non-public properties are not seen by the equality and hashing engine, because comparison and hashing run from outside the implementing class.

```
