Re: RFC: Comparable interface
| From: | Gustavo Lopes | Date: | Thu, 30 Sep 2010 15:13:40 +0000 |
| Subject: | Re: RFC: Comparable interface | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-49820@lists.php.net to get a copy of this message | ||
On Thu, 30 Sep 2010 14:33:05 +0100, Adam Harvey <aharvey@php.net> wrote:
I've just written an RFC (with a patch against trunk) to implement a Comparable interface similar to that in Java — in effect, allowing object instances to be compared with semantics defined in userspace. This is admittedly at the lower end of RFC-worthy proposals, but it's a good system, and I'd like to see it used a little more. Plus, it's good practice for the more interesting stuff to come. :) The RFC is at http://wiki.php.net/rfc/comparable, and the patch at http://www.adamharvey.name/patches/comparable.diff.txt. Any and all feedback welcome, particularly since I'm still learning my way around the innards of the Zend Engine, and it's entirely possible (indeed, likely) that I've overlooked something obvious. If there's no great resistance to the concept or implementation, I'm happy enough to flip this into the voting stage sooner rather than later so we can get it out of the way.I think this would be a good idea if limited to provide a userspace alternative to compare_objects, like Countable provides a userspace alternative to count_elements. That is, you would only modify zend_std_compare_objects (not compare_function!) and add there the additional logic where you'd check if the objects had the same class (or perhaps one was of a subclass of the other, in which case you'd use the compare method of the subclass) and call the compare method. Allowing comparison of objects with non-objects can become very confusing: * What if the objects are not of the same class? What if one is of a subclass of the other? Is equality not symmetric? * How do you interact with 'get' and 'cast_object'/'__tostring'? It's true that there's already overloading of '==' for internal classes, but it's very predictable: both operands must have the same 'compare_objects', otherwise it's not used. For the record, the current rules are relatively simple: * If both are objects and have the same 'compare_objects' handler, use it. * If the first operand is an object and has a 'get' (tried first) or 'cast_object' handler call it, and repeat with the new value. * Try the same for the second operand. -- Gustavo Lopes