Comments on: Immutable Value Objects in PHP http://bradley-holt.com/2010/09/immutable-value-objects-in-php/ Mon, 29 Sep 2014 19:50:55 +0000 hourly 1 https://wordpress.org/?v=4.6.1 By: Programowanie w PHP » Blog Archive » Bradley Holt’s Blog: Immutable Value Objects in PHP http://bradley-holt.com/2010/09/immutable-value-objects-in-php/comment-page-1/#comment-1447 Mon, 20 Dec 2010 16:44:39 +0000 http://bradley-holt.com/?p=863#comment-1447 […] to his blog about a subject he’s recently been learning about, Domain-Driven Design, and how immutable value objects could be useful in PHP. Yesterday I tweeted: Modern object-oriented programming languages need support for immutable […]

]]>
By: sokzzuka http://bradley-holt.com/2010/09/immutable-value-objects-in-php/comment-page-1/#comment-846 Sat, 02 Oct 2010 07:16:24 +0000 http://bradley-holt.com/?p=863#comment-846 The value objects could also be passed by copy like primitive types. Also if we had such objects, we could replace primitive types with them in namespaced code – like ‘use MyString as string’ (instead of autoboxing), so $foo = ‘bar’ would be equivalent to $foo = new MyString(‘bar’)

]]>
By: Bradley Holt http://bradley-holt.com/2010/09/immutable-value-objects-in-php/comment-page-1/#comment-839 Sat, 02 Oct 2010 00:05:43 +0000 http://bradley-holt.com/?p=863#comment-839 @Peter: Interesting scenario. I suppose the original object would be completely replaced in that situation so would not technically be immutable.

@sokzzuka: Yes, operator overloading would be a neat feature too. Would fit well with the “closure of operations” concept.

@Simon Harris: I’m not sure what this contract would look like in code, but the idea is similar to the contract you give when implementing an interface.

@Daniel O’Connor: I’ll write another blog post about how immutable Value Objects are useful. One benefit is that it gives you the flexibility of passing the object around without worrying about its state getting changed by some other code.

@Mikko Koppanen: I’m not familiar with PHP internals, so not sure how to read this. It looks like I would have to extend Immutable in order to have an immutable object. Interesting approach. Not quite what I was envisioning, but it does meet the requirements I outlined: the object is immutable and the fact that its an instance of Immutable provides a contract to client code that it’s immutable.

@Giorgio: Agreed. That’s exactly what I was getting at when I talked about trusting your teammates. There’s nothing currently preventing PHP developers from creating immutable Value Objects. Language support would be a nice-to-have.

]]>
By: Giorgio http://bradley-holt.com/2010/09/immutable-value-objects-in-php/comment-page-1/#comment-836 Fri, 01 Oct 2010 16:46:52 +0000 http://bradley-holt.com/?p=863#comment-836 The most important point to take out of this discussion, which Evans cited in the original DDD book, is that even if a concept does not exist at the language level, it is still valuable and can be adopted by a coscientious team of programmers.
“[…] the lack of direct language support for a conceptual distinction does not mean that the distinction is not useful. It just means that more discipline is needed to maintain the rules that will be only implicit in implementation. This can be reinforced with naming conventions, selective documentation, and lots of discussion.”
Unfortunately, our favorite language (PHP) is bloated with features like Late Static Bindings instead of supporting DDD concepts. 🙂

]]>
By: Mikko Koppanen http://bradley-holt.com/2010/09/immutable-value-objects-in-php/comment-page-1/#comment-834 Fri, 01 Oct 2010 12:03:59 +0000 http://bradley-holt.com/?p=863#comment-834 Hi,

Quick hack http://github.com/mkoppanen/php-immutable seems to work OK for this purpose.

]]>
By: Daniel O'Connor http://bradley-holt.com/2010/09/immutable-value-objects-in-php/comment-page-1/#comment-833 Fri, 01 Oct 2010 09:00:17 +0000 http://bradley-holt.com/?p=863#comment-833 I don’t get it: what design problems are there that need something immutable.

How are you as the author of the code able to know your immutable object really needs to be unchangeable in every possible imaginable scenario?

To put it more concisely, if it’s immutable; how are you meant to mock it out in unit tests? Or what do you gain by making it untestable?

]]>
By: Simon Harris http://bradley-holt.com/2010/09/immutable-value-objects-in-php/comment-page-1/#comment-832 Fri, 01 Oct 2010 08:49:08 +0000 http://bradley-holt.com/?p=863#comment-832 Hi, you mention several times that you require an “explicit contract that the object is immutable”, but you don’t give an example of a case where you would need it, or of how it might look and act in code. I wonder if those might help people see where you’re coming from?

]]>
By: sokzzuka http://bradley-holt.com/2010/09/immutable-value-objects-in-php/comment-page-1/#comment-830 Fri, 01 Oct 2010 07:22:04 +0000 http://bradley-holt.com/?p=863#comment-830 Excelent idea, I would also love to see operator overloading on those ValueObjects (but not on regular objects).

]]>
By: David http://bradley-holt.com/2010/09/immutable-value-objects-in-php/comment-page-1/#comment-828 Fri, 01 Oct 2010 04:21:02 +0000 http://bradley-holt.com/?p=863#comment-828 Actually, with Matthew’s example you don’t even need references as the values are stored as public properties.

This should work better:
http://paste2.org/p/1012059

]]>
By: Peter http://bradley-holt.com/2010/09/immutable-value-objects-in-php/comment-page-1/#comment-816 Thu, 30 Sep 2010 18:53:55 +0000 http://bradley-holt.com/?p=863#comment-816 I agree. A language feature would be the most solid approach. All other approaches either take too much effort or can be circumvented. For instance, Matthew’s approach (I am sure it was just a quick proof of concept) does not take references into account.

$v = new Immutable(array(‘foo’ => ‘bar’, ‘bar’ => ‘baz’));
$t =& $v->baz;
$t = 1;
var_export($v);

]]>