Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Value objects


Published on

A short introduction to the Value Object pattern in PHP. Use the type system to your advantage, ensure your values are actually valid.

Published in: Software
  • Login to see the comments

  • Be the first to like this

Value objects

  1. 1. Value Objects Make your code simpler
  2. 2. Who am I Product Developer - Systems Architect - Freelancer - Development Manager Past experience Senior Developer for Cleverbug Lecturer at the Digital Skills Academy Director of Tercet, software development consultancy Current position Software Development Manager for OliveMedia Barry O Sullivan
  3. 3. Design Patterns When developing, there are common patterns of problems. Design patterns are common solutions to those problems. The ValueObject is a simple design pattern that you can use right away.
  4. 4. The Problem public function changeName($firstname, $lastname) { if (!is_string($firstname) || $firstname == “”) { throw new InvalidNameException(“Firstname ‘$firstname’ is invalid”); } if (!is_string($lastname) || $lastname == “”) { throw new InvalidNameException(“Lastname ‘$lastname is invalid”); } $this->firstname = $firstname; $this->lastname = $lastname; } Have you ever written code like this?
  5. 5. Guarding This is called Guarding You are guarding against bad input The idea is, you can’t trust that other objects/developers are actually sending you valid data, you’re trying to protect against inconsistent state. Guarding is a defensive patterns and is encoraged in the book “The Mythical Man Month”.
  6. 6. Too much guarding The problem arises when you start adding this logic everywhere. You end up with ● Duplicated code ● Messy Logic ● Poor legibility Which leads to a system that’s very difficult to modify and understand. Leading to more bugs, not less. Developers
  7. 7. The Solution How do we solve this problem? With encapsulation! You create a class that represents the value. On creation, it gets passed the basic types and checks if they’re valid. ● If they are not, an exception is thrown. ● If they are, then the object is successfully created. That’s the basic gist of it.
  8. 8. Example, Name as a ValueObject class Name implements ValueObject { private $firstname; private $lastname; public function __construct($firstname, $lastname) { if (!is_string($firstname) || $firstname == “”) { throw new InvalidNameException(“Firstname ‘$firstname’ is invalid”); } if (!is_string($lastname) || $lastname == “”) { throw new InvalidNameException(“Lastname ‘$lastname is invalid”); } $this->firstname = $firstname; . . . . . .
  9. 9. Using the Name ValueObject //Controller method to change name public function changeName() { $user = $this->get_user($this->user_id); $name = new Name(Input::get(‘firstname’), Input::get(‘lastname’)); $user->changeName($name); } . . . . . . //Then in our User class public function changeName(Name $name) { $this->name = $name; }
  10. 10. ValueObjects represent the value Key Concept: A value object represents the value, not the data contained within it. You shouldn’t care about the internal data when using them for business logic. When you want to reference a value, use a value object. The only place the internals matter is when you’re encoding/decoding them for transmission (JSON/HTML) or storage (MySQL/REDIS), nowhere else.
  11. 11. Key features in the wild They are immutable They are comparable
  12. 12. Quick Recap: ValueObjects . . . ● Encapsulate a value ● Allow you to guard against bad input ● Represent the value, not the data within ● Are immutable ● Make code and system design much easier, leading to more structured system that’s easier to maintain.
  13. 13. More reading Thanks for listening If you’re interested in learning more about this pattern and others like it, I’d recommend Domain Driven Design.
  14. 14. Q&A “Judge a man by his questions rather than by his answers.” - Voltaire “I needed content for this slide, so I added quotes.” - Barry O Sullivan