Latest news and articles about DDD, CQRS, Event Sourcing and Software Architecture in general, aggregated daily and delivered weekly to your feed (RSS) reader. Subscribe here!

Week 7, year 2013

  • Casting Value Objects to strings - I’m a big fan of Value Objects, as they are very helpful in encapsulating behavior, and communicating intent. In fact, as a friend remarked, Value Objects are the heart and soul of Object Oriented Programming. Discussing the uses of Value Objects is not what I want to talk about here though. Let’s set up two simple examples first: <?phpclassTwitterHandle{private$handle;publicfunction__construct($handle){// Put some validation here, like a regex check$this->handle=$handle;}}classDateRange{private$start,$stop;publicfunction__construct(DateTime$start,DateTime$end){// Put some validation here, like checking if $end > $start$this->start=$start;$this->stop=$end;}} Simple string casting Value Objects are one of the few places where using PHP’s magic __toString() makes perfect sense. For single-value Value Objects like the TwitterHandle, it’s obvious what should be in there. The _toString() method simply returns the string representation. [Mathias Verraes]
Permalink | From 11 February 2013 to 17 February 2013 | Last updated on: Mon, 7 Jun 2021 09:18:52 GMT