You should only extend from the ValueObject class
<?php
/**
* Class Age
*/
class Age extends ValueObject
{
}
<?php
/**
* Class Age
*/
class Age extends ValueObject
{
}
$john = new Age(34);
$mario = new Age(34);
$raquel = new Age("34");
$john->equals($mario); // true
$john->equals($raquel); // false
$john->equals(new FirstName('Mario')); // false
$raquel->value(); // "34"
Override value() method to indicate the return primitive type.
<?php
/**
* Class Age
*/
class Age extends ValueObject
{
/**
* @return int
*/
public function value(): int
{
return parent::value();
}
}
$raquel = new Age("34");
$mario = new Age(34);
$raquel->value(); // int 34
$mario->equals($raquel); // true
(string)$mario; // string "34"
If your object can contain null value you can extend of NullableValueObject class
<?php
/**
* Class Age
*/
class Age extends NullableValueObject
{
}
// That's all! I hope this helps you