It'd be nice if, when doing $objA > $objB, that that'd invoke
$objA->__compareTo($objB) or something, much like Java's Comparable
interface.
It'd be nice if, when doing $objA > $objB, that that'd invoke
$objA->__compareTo($objB) or something, much like Java's Comparable
interface.
There is nothing stopping you from creating this in user-land. Also, I have
spent a lot of time writing libraries in PHP and I now believe that this
approach is not the best solution anyway.
It'd be nice if, when doing $objA > $objB, that that'd invoke
$objA->__compareTo($objB) or something, much like Java's Comparable
interface.There is nothing stopping you from creating this in user-land. Also, I
have spent a lot of time writing libraries in PHP and I now believe that
this approach is not the best solution anyway.
Except for the magic that happens on the comparison, I mean. I really
don't think PHP should go down that road at this point, if ever.
It'd be nice if, when doing $objA > $objB, that that'd invoke
$objA->__compareTo($objB) or something, much like Java's Comparable
interface.
I wrote https://wiki.php.net/rfc/comparable a couple of years ago —
there's a patch there that would probably still apply without too much
work to master. About the only difference was that I didn't double
underscore the magic method (in line with both Java and PHP interfaces
like Countable).
I ended up withdrawing it because the response at the time was
somewhere between "meh" and "outright hostility"; I didn't see much
point devoting time to something that was going to fail a vote
regardless. It could be dusted off and reproposed for 5.6 if there was
enough interest, but my guess is that it'd still be an uphill battle
(even though some internal classes, most notably DateTime, do exactly
this).
Adam
Hi!
I wrote https://wiki.php.net/rfc/comparable a couple of years ago —
there's a patch there that would probably still apply without too much
work to master. About the only difference was that I didn't double
underscore the magic method (in line with both Java and PHP interfaces
like Countable).
Overriding < and > isn't the biggest issue, even though BC issues with
conversions may definitely be a surprise. Bug biggest one is ==, which
may have a lot of very non-trivial effects if you make == return
"equals" when other functions (such as searches, hashes, etc.) may treat
them as not equal. It is quite a complex thing which is rife with
unexpected side effects, so I think it would be better if it were explicit.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
To me, it sounds that those extremely specific cases ask for a extremely
specific compareTo(stdClass) method.
Daniel Ribeiro Gomes Pereira
Twitter https://twitter.com/#!/drgomesp |
Facebookhttps://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 Stas Malyshev smalyshev@sugarcrm.com
Hi!
I wrote https://wiki.php.net/rfc/comparable a couple of years ago —
there's a patch there that would probably still apply without too much
work to master. About the only difference was that I didn't double
underscore the magic method (in line with both Java and PHP interfaces
like Countable).Overriding < and > isn't the biggest issue, even though BC issues with
conversions may definitely be a surprise. Bug biggest one is ==, which
may have a lot of very non-trivial effects if you make == return
"equals" when other functions (such as searches, hashes, etc.) may treat
them as not equal. It is quite a complex thing which is rife with
unexpected side effects, so I think it would be better if it were explicit.--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
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
Its kinda useless feature for PHP.
Daniel Ribeiro Gomes Pereira
Twitter https://twitter.com/#!/drgomesp |
Facebookhttps://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
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
Classes without the ability to overload the comparison operator could be
considered kinda useless as well.
Its kinda useless feature for PHP.
Daniel Ribeiro Gomes Pereira
Twitter https://twitter.com/#!/drgomesp |
Facebookhttps://www.facebook.com/profile.php?id=100000407054469
| LinkedIn http://www.linkedin.com/pub/daniel-ribeiro-gomes/21/414/39
iPhone: +55 (48) 9111-09312013/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
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
Classes without the ability to overload the comparison operator could be
considered kinda useless as well.Its kinda useless feature for PHP.
Daniel Ribeiro Gomes Pereira
Twitter https://twitter.com/#!/drgomesp |
Facebookhttps://www.facebook.com/profile.php?id=100000407054469
| LinkedIn http://www.linkedin.com/pub/daniel-ribeiro-gomes/21/414/39
iPhone: +55 (48) 9111-09312013/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
Hi!
Classes without the ability to overload the comparison operator could be
considered kinda useless as well.
That's demonstrably false - classes are very useful right now in PHP yet
no overloading of operators exists.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
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.
bignum PHP class:
http://phpseclib.sourceforge.net/math/intro.html
Per the benchmarks it's internal implementation (without OpenSSL) is faster
than BCMath is in 5.4 lol. Not sure which OS the test was ran on.
That's the only use case I can come up with off the top of my head but
that's more than I can come up with for Iterator atm lol.
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
I would like to see this feature in PHP! I often thought it would be very helpful. In days of PHP 5.2 I wrote a Date-Class similar to DateTime but with another API which should be the same as in C#. Comparing dates with $date->compareTo($date2) > 0
is always hard to read and error-prone.
Another example: You have one database entity and a collection and you want to check if it is in the collection.
$dbEntity = ...;
$dbCollection = ...;
foreach ($dbCollection as $tmpEntity) {
if ($dbEntity->getId() == $tmpEntity->getId()) {
// ...
}
}
Instead you could write $dbEntity == $tmpEntity
. I think this is easier to read.
Of course you can say "just syntetic sugar", but better readability of code helps developers a lot!
Best regards
Christian