Proposed operator overloading and magic methods to compare with other objects is very good idea, I like it a lot, there even may be additional restriction provided with my other RFC https://wiki.php.net/rfc/object-typehint
But I think this goal should be provided in separate RFC as it is very usefull in immutable objects, but also will be in mutable objects. While this is not required to introduce immutable objects I thinkk it should be in separate RFC. 2016-09-13 8:40 GMT+02:00 Fleshgrinder <p...@fleshgrinder.com>: > Thanks a lot for your feedback. > > I like the clone idea too and I also see no harm in allowing them in any > context rather than only in immutable classes. I am really against the > transformer keyword, don't ask me why, I just don't like it at all. :P > > I mentioned before that PHP should allow for operator overloading since > there are other value object use cases that are even simpler than your > HTTP header result: any value object that is case insensitive. Doh! > > I do like the https://wiki.php.net/rfc/comparable RFC but we would need > more for value objects to be truly useful. > > class A { > > // $this == $other > function __isEqual($other); > > // $this === $other > function __isIdentical($other); > > } > > This will be very hard to get through though because it's operator > overloading. I am currently already doing this in many of my value > objects, although I usually implement an equals method only and let it > take care of equals and identity checks at the same time. > > final class HexNumber { > > private $val; > > public function __construct(string $hex) { > $this->val = strtolower($hex); > } > > public function equals($other) { > if ($other instanceof self) { > return $other->val === $this->val; > } > > if (is_string($other)) { > return strtolower($other) === $this->val; > } > > return false; > } > > } > > $hn = new HexNumber('ff00'); > var_dump($hn->equals('FF00')); // bool(true) > > However, this is not necessarily exactly what you want in every context, > hence, the proposal for operator overloading. > > That being said, I agree that this could (and probably should) be > tackled in another RFC. Immutable classes do not have to solve every > problem. > > -- > Richard "Fleshgrinder" Fussenegger > > -- regards / pozdrawiam, -- Michał Brzuchalski brzuchalski.com