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

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to