What is an example of a Value Object in PHP?
Asked Answered
P

2

9

I have read plenty of C# examples of Value Objects, and I understand that it is an "object" that is identified by it's values. If a value changes, the object is "new".

However, that doesn't seem to make sense when it comes to PHP...either that, or I'm just not making the connection.

Is a Value Object just a string?

Psilocybin answered 23/3, 2012 at 6:6 Comment(5)
Not everything in PHP is an object.Colorblind
I realize that. I'm just trying to make sense of some tutorials I've been reading that speak of Value Objects.Psilocybin
But not everything in PHP is an object, which results in the comparison being invalid.Colorblind
If I am reading "language agnostic" tutorials, what should I replace the phrase "Value Object" with in my mind? When someone says a Person object has an Address Value object - should I assume that in my world that simply translates to a property inside the Person? Thanks :)Psilocybin
@Ignacio Vazquez-Abrams: not everything in C# is an object either, and that has absolutely nothing to do with this question.Daudet
S
29

To put this into context, in many OO languages, objects are compared by their identity. In pseudocode:

bar = new Foo
baz = new Foo

bar == baz  // false

Even though both objects are basically the same if you just look at their values, they're not considered to be identical, because they are separate instances. To demonstrate:

bar = new Foo
baz = bar

bar == baz  // true

Now:

In computer science, a value object is a small simple object, like money or a date range, whose equality isn't based on identity.

http://en.wikipedia.org/wiki/Value_object

This would be a demonstration of "value objects":

address1 = new Address('Main street 42')
address2 = new Address('Main street 42')

address1 == address2  // true

Because the values are the same, both objects are considered equal, even if they're separate instances.

PHP does not have a separate concept of "value objects", it only has one type of object. Its comparison operator can make that distinction though:

When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class.

http://www.php.net/manual/en/language.oop5.object-comparison.php

E.g.:

$address1 = new Address('Main street 42');
$address2 = new Address('Main street 42');

$address1 == $address2;  // true     equal...
$address1 === $address2;  // false   ...but not identical
Southpaw answered 23/3, 2012 at 7:4 Comment(2)
@johnnietheblack: Note that in C# the distinction also rests mainly on the comparison method Equals(), which on System.Object compares for identity, but which System.ValueType overrides to compare content.Daudet
There are a lot of repositories that makes VO in PHP. This is my implementation: github.com/sensorario/value-objectExecution
A
0

PHP used to have value objects but that was back in PHP4. See http://3v4l.org/ghI8G

Ankylostomiasis answered 12/10, 2014 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.