PHPackages                             yomy/valueobject - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. yomy/valueobject

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yomy/valueobject
================

Value objects

3.0.4(7y ago)07.5k1Apache-2.0PHPPHP &gt;=7.0

Since Sep 20Pushed 7y ago1 watchersCompare

[ Source](https://github.com/yomy/valueobject)[ Packagist](https://packagist.org/packages/yomy/valueobject)[ Docs](https://github.com/yomy/valueobject)[ RSS](/packages/yomy-valueobject/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (7)Dependencies (1)Versions (9)Used By (0)

valueobject
===========

[](#valueobject)

Value Objects and Enums

[![Build Status](https://camo.githubusercontent.com/b7ff93c6ea6876c4b0ab85a6fbba6ababe992fd1be757acd1fc9b413a2922c4f/68747470733a2f2f7472617669732d63692e6f72672f796f6d792f76616c75656f626a6563742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/yomy/valueobject)

Installation and documentation
------------------------------

[](#installation-and-documentation)

- Available as \[Composer\] package \[yomy/valueobject\].

What is this library about
--------------------------

[](#what-is-this-library-about)

This library adds a ValueObject and EnumValueObject classes.

Examples of value object
------------------------

[](#examples-of-value-object)

Creating an object:

```
use YomY\ValueObject\ValueObject;
$object = ValueObject::instance(1);
```

Getting value from the object

```
$value = $object->getValue();
```

Objects with the same value are going to be the same object

```
$object1 = ValueObject::instance(1);
$object2 = ValueObject::instance(1);
//These two are the same objects ($object1 === $object2)
```

You can use type hinting in methods

```
public function doSomething(ValueObject $valueObject) {
    $value = $valueObject->getValue();
    ...
}
```

You can extend the object for more detailed type hinting

```
class UserId extends ValueObject {}
class DataId extends ValueObject {}
...
public function doSomething(UserId $userId, DataId $dataId) {
    ...
}
```

Objects of different class or variable type are different

```
$object1 = ValueObject::instance('');
$object2 = ValueObject::instance(null);
$object3 = ValueObject::instance(false);
$object4 = ExtendedValueObject::instance('');
$object5 = ExtendedValueObject::instance(null);
$object6 = ExtendedValueObject::instance(false);
//All of the above are different
```

Instead of a strong (===) comparison operator, you could also use the equals() method

```
$object1 = ValueObject::instance(1);
$object2 = ValueObject::instance(1);
$same = $object1->equals($object2); //true
```

Generally, unserialize of a value object is prohibited, as this would break the ability to compare objects by reference. However, you might have a case you don't care about strict comparison, and need to unserialize the object. You can add a WeakValueObjectTrait usage to your custom object, which will allow unserializing it, and also compare objects by the values instead of reference when using equals() method

```
class MyWeakObject extends ValueObject {
    use WeakValueObjectTrait;
}
...
$weakObject1 = MyWeakObject::instance(1);
$serializedWeakObject = serialize($weakObject1);
$weakObject2 = unserialize($serializedWeakObject);
//These two objects are "equal" but not the same
$weakObject1->equals($weakObject2); //true
//On regular value objects this would be false
```

Examples of Enum value object
-----------------------------

[](#examples-of-enum-value-object)

Creating an enum object

```
use YomY\ValueObject\EnumValueObject;
class Category extends EnumValueObject {
    const FIRST = 1;
    const SECOND = 2;
    const THIRD = 3;
}
```

Creating enum objects

```
$category = Category::instance(Category::FIRST);
```

or by referring referring to key

```
$category = Category::FIRST();
```

You will get an error if you try to instantiate invalid value

```
$category = Category::instance('missing_value');
$category = Category::MISSING();
```

Examples of Positive Integer value object
-----------------------------------------

[](#examples-of-positive-integer-value-object)

As value objects are commonly used as identifiers for database entities with an integer key, positive int value object ensures a valid key object for this purpose

Creating an object:

```
use YomY\ValueObject\PositiveIntValueObject;
$object1 = PositiveIntValueObject::instance(1);
$object2 = PositiveIntValueObject::instance('1');
//These two are the same objects ($object1 === $object2)
```

Usually, the id key in the db cannot be a 0, so these objects are invalid:

```
$object = PositiveIntValueObject::instance(0);
$object = PositiveIntValueObject::instance('0');
```

Of course, as with a basic value object, it is intended to use these in extended classes.

```
class UserId extends PositiveIntValueObject {}
class DataId extends PositiveIntValueObject {}
$user = UserId::instance(42);
$data = DataId::instance(42);
//these two are not the same
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity63

Established project with proven stability

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 ~90 days

Recently: every ~53 days

Total

7

Last Release

2613d ago

Major Versions

1.0 → 2.02018-06-22

2.0 → 3.02018-08-20

PHP version history (2 changes)1.0PHP &gt;=5.3.0

2.0PHP &gt;=7.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3044904?v=4)[yomy](/maintainers/yomy)[@yomy](https://github.com/yomy)

---

Tags

enumValue ObjectdddValueObject

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/yomy-valueobject/health.svg)

```
[![Health](https://phpackages.com/badges/yomy-valueobject/health.svg)](https://phpackages.com/packages/yomy-valueobject)
```

###  Alternatives

[moneyphp/money

PHP implementation of Fowler's Money pattern

4.8k82.5M422](/packages/moneyphp-money)[myclabs/php-enum

PHP Enum implementation

2.7k227.9M637](/packages/myclabs-php-enum)[dasprid/enum

PHP 7.1 enum implementation

379146.0M11](/packages/dasprid-enum)[spatie/enum

PHP Enums

84429.1M68](/packages/spatie-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49444.8M97](/packages/marc-mabe-php-enum)[oskarstark/enum-helper

This library provides helpers for several enum operations

132.9M17](/packages/oskarstark-enum-helper)

PHPackages © 2026

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