Re: idea: implement a Comparable interface

From: Date: Tue, 07 May 2013 19:15:08 +0000
Subject: Re: idea: implement a Comparable interface
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-67348@lists.php.net to get a copy of this message
I proposed something similar recently in the PHP-FIG group, but decided that without the language-level ability to overload comparison operators, standardizing comparables ony using methods was not very useful. If PHP could provide a magic method for comparison that involved being able to use comparison operators, I think this feature would be really useful. An example that comes to mind is a Money class: $pesos = new Money(200, Money::PESO); $dollars = new Money(100, Money::US_DOLLAR); var_dump($pesos < $dollars); Then, the Money class could take care of normalizing and comparing the values. Food for thought... -- Jake On May 7, 2013, at 1:15 PM, Stuart Langley <slangley@google.com> wrote: > Classes without the ability to overload the comparison operator could be > considered kinda useless as well. > > > On Tue, May 7, 2013 at 11:11 AM, Daniel Ribeiro <drgomesp@gmail.com> wrote: > >> Its kinda useless feature for PHP. >> >> >> Daniel Ribeiro Gomes Pereira >> Twitter <https://twitter.com/#!/drgomesp> | >> >> Facebook<https://www.facebook.com/profile.php?id=100000407054469> >> | LinkedIn >> <http://www.linkedin.com/pub/daniel-ribeiro-gomes/21/414/39> >> iPhone: +55 (48) 9111-0931 >> >> >> 2013/5/7 Nikita Popov <nikita.ppv@gmail.com> >> >>> On Tue, May 7, 2013 at 6:17 PM, Thomas Anderson <zelnaga@gmail.com> >> wrote: >>> >>>> It'd be nice if, when doing $objA > $objB, that that'd invoke >>>> $objA->__compareTo($objB) or something, much like Java's Comparable >>>> interface. >>>> >>> >>> Do you have examples of what this would be useful for? The two things >> that >>> come to mind are DateTime (which can do this anyway as it's an internal >>> class) and classes for bignums or something like that (which are probably >>> also better implemented internally). So I'm not sure how much use there >> is >>> for this. >>> >>> Nikita >>> >>

« previous php.internals (#67348) next »