Beyond Basic Data Types: Exploring Entities and Value Objects

jochelle mendonca
3 min readMar 25, 2024

--

Lego | Value Object | Entities | Programming
Photo by Sen on Unsplash

Ah, data modeling in PHP — sometimes it feels like wrangling a herd of digital cats! Especially when you’re staring at a class and unsure if it’s an entity or a value object.

Let me tell you, I’ve been there.

Early on in my PHP journey, I was working on a project where a senior developer suggested I change a particular class from a value object to an entity.

Now, back then, these terms were like alien languages to me.

Entities? Value objects? They both held data, right? What was the big deal?

Well, that developer saw the confusion plastered across my face and took some time to explain the difference. It was a real “aha!” moment for me. Here’s what I learned (and what I wish I knew back then):

Imagine Your Data as Building Blocks:

Think of your data as building blocks for your application. Some of these blocks are like LEGO minifigs — they’re unique individuals with distinct roles and stories.

These are your entities.

They have a lifespan, can change over time (think a user changing their profile picture), and often have relationships with other entities (a user creates orders).

Value Objects: The Simple, Immutable Blocks:

On the other hand, some data blocks are more like basic LEGO bricks themselves. They represent simple, unchanging values that describe something.

These are your value objects.

Think of them as the color or shape of a brick — they contribute to the whole picture but don’t have an individual story.

My Experience: From Confusion to Clarity:

So, back to me and my class that I had designed then.

It turned out it was representing an Address — a user’s street, city, and postal code. At first glance, it seemed like a simple value object. But the senior developer pointed out something crucial: an address could potentially change for a user (entity) over time.

That’s where the distinction became clear.

An address might be a simple set of data, but it describes an entity (the user) and has the potential to be modified. So, it was more appropriate to model it as an entity.

Let’s Code for Clarity!

Here are some code examples to illustrate the difference:

Entity:

class User {
private $id;
private $name;
private $email;
private $address; // This can be an entity itself containing address details

// Methods for setting, getting, and manipulating user data (e.g., updating address)
}

A User entity represents a complex concept with a unique ID, name, email, and potentially, an Address entity describing their location.

Value Object: (Let’s say we have a separate Product entity)

class Price {
private $amount;
private $currency;

// Constructor to set the initial values (immutable)
public function __construct(float $amount, string $currency) {
$this->amount = $amount;
$this->currency = $currency;
}
// Methods to access the price components (no modification)
}

A Price value object represents a simple, unchanging piece of data. It describes a Product's price but doesn't have its own identity.

Choosing the Right Tool:

So, when do you use which? Here’s a quick recap:

  • Entities: Use entities for complex, unique things with a lifecycle and relationships.
  • Value Objects: Use value objects for simple, immutable data that describes something without needing its own identity.

In Summary…Key Differences: Entities vs. Value Objects

  1. Identity vs. Attributes: Entities have distinct identities and are defined by unique identifiers, while value objects are characterized by their attributes and lack individual identities.
  2. Mutability vs. Immutability: Entities are mutable and can change over time, whereas value objects are immutable and maintain their state once instantiated.
  3. Equality: Entities are compared based on identity, while value objects are compared based on the equality of their attributes.

Understanding these concepts has made my data modeling so much smoother.

Remember, using the right data structures keeps your code organized and easier to understand, not just for you but for future developers too!

Happy coding, and don’t be afraid to ask questions — even the seemingly basic ones can lead to big learning moments.

If you liked this article and would like to support me, make sure to:

  • 👏Clap for this story
  • 🔔Follow me on Medium
  • ☕️ If you feel my work is worth an appreciation, you can buy me a coffee!

--

--

jochelle mendonca

Passionate PHP developer. Enthusiastic about the power of words, equally adept at reading and writing