Version: PDF
Chapter: Value Object
Section: Side-Effect-Free Behavior
Page: 54 - 55
In the two partial Money Class examples given, is it correct in example 1 to have the condition $money->currency() !== $this->currency()? Should it not be the same as example 2$money->currency()->equals($this->currency())?
Example 1 from page 54
class Money
{
// ...
public function add(Money $money)
{
if ($money->currency() !== $this->currency()) {
throw new \InvalidArgumentException();
}
$this->amount += $money->amount();
}
}
Example 2 from page 55
class Money
{
// ...
public function add(Money $money)
{
if(!$money->currency()->equals($this->currency())) {
throw new \InvalidArgumentException();
}
// start-of-new-code-to-take-a-look
return new self(
$this->amount() + $money->amount(),
$this->currency()
);
// end-of-new-code-to-take-a-look
}
}
Version: PDF Chapter: Value Object Section: Side-Effect-Free Behavior Page: 54 - 55
In the two partial Money Class examples given, is it correct in example 1 to have the condition
$money->currency() !== $this->currency()
? Should it not be the same as example 2$money->currency()->equals($this->currency())
?Example 1 from page 54
Example 2 from page 55