> Those of you with long memories will remember that I proposed a > Comparable interface way back in the pre-5.4 days, but withdrew it > when it became obvious that there was no consensus for it as a feature > and that a vote was likely to fail. > > RFC: https://wiki.php.net/rfc/comparable > PR: https://github.com/php/php-src/pull/1097 > > Why reanimate it now, I hear you ask? I think that comparisons have > only become more prominent in the language: we now have a spaceship > operator for explicit comparisons, yet the behaviour of object > comparisons can be obscure, to say the least, and the user has no > control over how their objects are compared. > > At this stage, I intend to put this up for a vote on March 5 (vote > ending March 12), with the obvious endgame being that this would be > included in 7.0.
If the responsibility of comparing two objects is pushed into the objects themselves then the ability to use different comparison criteria for the same two objects when placing them in two different structures is taken away. This is actually a very common problem I have seen people complain about in Java. Another issue: it allows comparing an object to non-objects (even though the stated goal is only to compare two objects of the same type): class MyClass implements Comparable { private $val = 0; function compareTo($a) { return $this->val <=> $a; } } $int = 10; $myClass = new MyClass(); $myClass <=> $int; // works $int <=> $myClass; // if this doesn't produce a warning it at least doesn't behave the same as the line above it Ultimately, when comparing two objects it is usually as part of some algorithm, so instead the Comparator interface could be used: interface Comparator { function compare($a, $b): int; } But even here I would rather just take a function instead of requiring it to be the instance of some interface: function sort($input, callable $comparator($a, $b): int) { /* … */ } All in all, I don't think the fundamental idea of the RFC is good. The Comparable interface is inferior to many other techniques of achieving the same goal of comparing two objects. I do want to thank you for taking the time to cite arguments, prior history and an alternative in the RFC. You have done a pretty good job on the RFC itself, in my opinion. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php