public function __construct($basketId) { $this->basketId = (string) $basketId; } public static function fromString($string) { return new BasketId($string); } public function __toString() { return $this->basketId; } public function equals(IdentifiesAggregate $other) { return $other instanceof BasketId && $this->basketId == $other->basketId; } // A nice convention is to have a static generate() method to create a random BasketId. The example here is a bad // way to generate a UUID, so don't do this in production. Use something like https://github.com/ramsey/uuid public static function generate() { $badSampleUuid = md5(uniqid()); return new BasketId($badSampleUuid); } } // Sample usage: $basketId = BasketId::fromString('12345678-90ab-cdef-1234-567890abcedf1234'); // Casting to string gives you the original string back: it("should cast to string", (string) $basketId == '12345678-90ab-cdef-1234-567890abcedf1234'); // Testing equality: it("should equal instances with the same type and value", (new BasketId('same'))->equals(new BasketId('same'))); it("should not equal instances with a different value", !(new BasketId('other'))->equals(new BasketId('same')));