PHPackages                             phpgears/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. phpgears/value-object

ActiveLibrary

phpgears/value-object
=====================

Value object for PHP

0.2(6y ago)01.3kMITPHPPHP ^7.1CI failing

Since Sep 16Pushed 5y ago1 watchersCompare

[ Source](https://github.com/phpgears/value-object)[ Packagist](https://packagist.org/packages/phpgears/value-object)[ Docs](https://github.com/phpgears/value-object)[ RSS](/packages/phpgears-value-object/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (2)Dependencies (15)Versions (5)Used By (0)

[![PHP version](https://camo.githubusercontent.com/d0b5687c6812c5d52d86a548e09db527eeb7860f82adbb677de00a36ddbed1b4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253345253344372e312d3838393242462e7376673f7374796c653d666c61742d737175617265)](http://php.net)[![Latest Version](https://camo.githubusercontent.com/a1379b2d828502462bd89535382a51e5186146f2a6b473a3f3ea258664e7b65c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70687067656172732f76616c75652d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phpgears/value-object)[![License](https://camo.githubusercontent.com/dc642e48e9dad073348f9db67779e895fa24b93f0f17a6bc20349400a70d0285/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f70687067656172732f76616c75652d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://github.com/phpgears/value-object/blob/master/LICENSE)

[![Build Status](https://camo.githubusercontent.com/ec7897804175d3ce5d9dced357edcec7439543d5ebd333c9645faae8e9339623/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f70687067656172732f76616c75652d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.com/github/phpgears/value-object)[![Style Check](https://camo.githubusercontent.com/85158094548ee78e21bd8a6deb49f3b1c052333200276907ff7aad1c0d27de32/68747470733a2f2f7374796c6563692e696f2f7265706f732f3134393033373530302f736869656c64)](https://styleci.io/repos/149037500)[![Code Quality](https://camo.githubusercontent.com/0393d1a85cd7b2c36e9703bd518ded6100ee605e23111d80d4bbdddcb9392d7f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f70687067656172732f76616c75652d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/phpgears/value-object)[![Code Coverage](https://camo.githubusercontent.com/344c20986e697dc889c5da6b83e914e4d7eb94e8ad1b840ad6af317f9fa67ba5/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f70687067656172732f76616c75652d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/github/phpgears/value-object)

[![Total Downloads](https://camo.githubusercontent.com/f1b453481e28a428cb3876c012a95d92a621ee599f5ca06b3c25d88f633c9e8f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70687067656172732f76616c75652d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phpgears/value-object/stats)[![Monthly Downloads](https://camo.githubusercontent.com/c6c44c6848c6517a1b708a2cb39d374e725a50066b42b1d06202a9e544cb77e0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f70687067656172732f76616c75652d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phpgears/value-object/stats)

value-object
============

[](#value-object)

Immutable Value Objects for PHP

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

[](#installation)

### Composer

[](#composer)

```
composer require phpgears/value-object

```

Usage
-----

[](#usage)

Require composer autoload file

```
require './vendor/autoload.php';
```

Extend from `Gears\ValueObject\AbstractValueObject`. Make your class final, value objects should always be final

Be aware of the protected constructor, you should create a "named constructor" for your value object

```
use Gears\ValueObject\AbstractValueObject;

final class CustomStringValueObject extends AbstractValueObject
{
    private $value;

    public static function fromString(string $value)
    {
        $stringObject = new self();
        $stringObject->value = $value;

        return $stringObject;
    }

    public function getValue(): string
    {
        return $this->value;
    }

    public function isEqualTo($valueObject): bool
    {
        return \get_class($valueObject) === self::class
            && $valueObject->getValue() === $this->value;
    }
}
```

### Serialization

[](#serialization)

Extending AbstractValueObject does not automatically define serialization mechanisms in your value objects because value objects can be composed of several values, other value objects and even other objects such as enums

For this consider adding serialization methods in your value objects to control how serialization takes place

```
use Gears\ValueObject\AbstractValueObject;

final class Money extends AbstractValueObject implements \Serializable
{
    private const CURRENCY_EUR = 'eur';

    private $value;
    private $precision;
    private $currency;

    public static function fromEuro(int $value, int $precision)
    {
        $money = new self();
        $money->value = $value;
        $money->precision = $precision;
        $money->currency = static::CURRENCY_EUR; // Should be an enum

        return $money;
    }

    // [...]

    final public function __serialize(): array
    {
        return [
            'value' => $this->value,
            'precision' => $this->precision,
            'currency' => $this->currency,
        ];
    }

    final public function __unserialize(array $data): void
    {
        $this->assertImmutable();

        $this->value = $data['value'];
        $this->precision = $data['precision'];
        $this->currency = $data['currency'];
    }

    final public function serialize(): string
    {
        return serialize([
            $this->value,
            $this->precision,
            $this->currency,
        ]);
    }

    public function unserialize($serialized): void
    {
        $this->assertImmutable();

        list(
            $this->value,
            $this->precision,
            $this->currency
        ) = \unserialize($serialized, ['allowed_classes' => false]);
    }
}
```

Enums and Value Objects get along perfectly, consider using [phpgears/enum](https://github.com/phpgears/enum) for enumerations

Contributing
------------

[](#contributing)

Found a bug or have a feature request? [Please open a new issue](https://github.com/phpgears/value-object/issues). Have a look at existing issues before.

See file [CONTRIBUTING.md](https://github.com/phpgears/value-object/blob/master/CONTRIBUTING.md)

License
-------

[](#license)

See file [LICENSE](https://github.com/phpgears/value-object/blob/master/LICENSE) included with the source code for a copy of the license terms.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~123 days

Total

4

Last Release

2421d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4c50421f1ab4148354dc2dd5dcaba168656b17ea913b310d112deb39a6f73ca1?d=identicon)[juliangut](/maintainers/juliangut)

---

Top Contributors

[![juliangut](https://avatars.githubusercontent.com/u/1104131?v=4)](https://github.com/juliangut "juliangut (11 commits)")

---

Tags

Value Objectimmutable

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phpgears-value-object/health.svg)

```
[![Health](https://phpackages.com/badges/phpgears-value-object/health.svg)](https://phpackages.com/packages/phpgears-value-object)
```

###  Alternatives

[moneyphp/money

PHP implementation of Fowler's Money pattern

4.8k82.5M421](/packages/moneyphp-money)[darsyn/ip

An immutable IP Address value object that provides several different notations, including helper functions.

2572.0M20](/packages/darsyn-ip)[aeon-php/calendar

PHP type safe, immutable calendar library

2079.7M16](/packages/aeon-php-calendar)[innmind/immutable

Immutable PHP primitive wrappers

75218.0k73](/packages/innmind-immutable)[rtlopez/decimal

An object oriented immutable arbitrary-precision arithmetic library for PHP

27262.8k2](/packages/rtlopez-decimal)[aeon-php/business-hours

Abstraction allowing to define and check against business hours

10135.8k](/packages/aeon-php-business-hours)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
