This post isn't about obvious advantages of representing domain logic with support of Value Object. As well, we wouldn't elaborate here about pros and cons of immutable objects. I'd rather would like to show an attempt to change Value Object, keeping it still immutable and using one of most bizarre, in my opinion, feature of PHP language, which is accessing private fields from outside an object.
Often times you would like to alter Value Object, by creating new one based on current (which is only valid way). Altering logic conceptually belongs to Value Object class, so should be located there. In such method, you clone current instance, set new information to given field and return the copy. And this is were accessing private fields for same class makes sense. You can do that without having additional setters which will break the design.
As illustrated above, all Value Object features are in place. This example is of course trivial, but you can imagine a lot more complicated VO's, like values shared between bounded contexts.
No need to call it "EmailValueObject" in my opinion :) Just "Email" is all you need, makes the code more expressive and readable. If you still want to mark it somehow, use an annotation or an (empty) ValueObject interface.
Nah :-) I use them all the time.
That is no "bizzare trick". Instead, accessing the "privates" of another instance of the same class is very common behaviour in OOP. Java and C++ behave the same, for example.
If you think about it, it doesn't break the encapsulation principle, since you are accessing the internals of an object from the class of that object.
The latter is a bug in my opinion, which has been reported by me here. But the PHP core developers won't fix it.
I've implemented a correct implementation of the Immutable Object implementation pattern as a reusable component using an interface and a trait, which requires PHP >= 5.4.
That interface is a so-called Marker Interface, as already mentioned in the comment of Mathias Verraes. I also recommend to rename that class to EmailAddress and place it into an appropriate namespace.
The source code is released under the LGPL-3.0+ license on GitHub here.
I also wrote a blog post (in German language) last year, which can be found here.
Since your blog post is linked on PHPDeveloper, I recommend that you correct the information in your post. Otherwise your posts may confuse other beginners.
competence with complex and unique online business ideas.
development is the key to the success of your project.
This is Great wonderful ideas.. Awesome ideas thanks..
First of all thank you for sharing this informative blog.. This concept explanation are very easy and step by step so easy to understand and also learnt this concept clearly from this article..