A Value Object (VO) is an immutable type that is only distinguishable by the state of its properties, that is, unlike an entity, which has a unique identifier and remains distinct even if its properties are identical, VOs with the same properties can be considered the same.
Because they are immutable, VOs cannot be changed once created. Modifying one is conceptually the same as discard the old one and create a new one.
More details about VOs.
composer require tiny-blocks/value-object
The library exposes available behaviors through the ValueObject
interface, and the implementation of these behaviors
through the ValueObjectAdapter
trait.
With the implementation of the ValueObject
interface, and the ValueObjectAdapter
trait, the use of
__get
, __set
and __unset
methods is suppressed, making the object immutable.
<?php
namespace Example;
use TinyBlocks\Vo\ValueObject;
use TinyBlocks\Vo\ValueObjectBehavior;
final class TransactionId implements ValueObject
{
use ValueObjectBehavior;
public function __construct(private readonly string $value)
{
}
}
The equals
method compares the value of two VOs, and checks if they are equal.
$transactionId = new TransactionId(value: 'e6e2442f-3bd8-421f-9ac2-f9e26ac4abd2');
$otherTransactionId = new TransactionId(value: 'e6e2442f-3bd8-421f-9ac2-f9e26ac4abd2');
$transactionId->equals(other: $otherTransactionId); # true
Value Object is licensed under MIT.
Please follow the contributing guidelines to contribute to the project.