Steve Grunwell

Open-source contributor, speaker, and electronics tinkerer

Tag: Value Objects

A dimly-lit, empty factory building

Factory Methods for Hydrating Objects from JSON

If you’ve worked in a lot of codebases, this scenario will be familiar: somewhere in the app, we’re JSON-decoding a string, then using that to pass arguments to a method (such as a constructor). It may look something like this:

$json = '{"first": "Steve", "last": "Grunwell"}';
$data = json_decode($json);

$person = new Person($data->first, $data->last);

I’d like to humbly ask that you stop doing this. Instead, this post is going to show you how to accomplish the same result with a static, factory method and explain why the latter approach will save you all sorts of headaches.

Continue reading→

Taco, a black cat, peeking out of a white drawer

The Beauty of PHP Value Objects

At last year’s php[tek], one of my biggest “holy cow, why haven’t I been doing this?!” moments came from my friend Andrew Cassell when he explained PHP Value Objects in the context of Domain-Driven Design.

Put simply, a Value Object is an immutable object that encapsulates some data and will always be in a valid state.

Continue reading→

Be excellent to each other.