Re: RFC: Comparable interface

From: Date: Sun, 03 Oct 2010 23:32:19 +0000
Subject: Re: RFC: Comparable interface
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-49858@lists.php.net to get a copy of this message
On Mon, 04 Oct 2010 00:18:36 +0100, Stas Malyshev <smalyshev@sugarcrm.com> wrote:
* The current behavior for>,<, etc. is completely useless. It's unpredictable and it doesn't even establish a total order: $a = new stdclass; $a->prop = null; $b = new stdclass; $b->prop2 = null; var_dump($a> $b); //false var_dump($a == $b); //false var_dump($b> $a); //false
That's because there's no total ordering of generic objects that can make sense. Only very specific objects - such as ones representing numeric qualities or having numeric properties - can be ordered, most of objects are unordered. And comparing objects with scalars automagically might bring a lot of surprises as nobody really expects $a == $b and $b == $a to be different now.
I never defended comparison of objects against scalars (though this is already possible, in a limited sense). Perhaps I ought to have changed the subject. I'm just saying that the current comparisons for user classes is either very limited (==) or completely useless (<, >, >=, <=). Given that, we could implement a *limited* mechanism for overloading those operators in user-land. If limited to objects of the same class (or with one operand of a subclass of the other), this could actually make the comparison operators useful with little potential for damage. It would also not be unprecedented since it's already allowed for internal classes. -- Gustavo Lopes

« previous php.internals (#49858) next »