RE: [PHP-DEV] idea: implement a Comparable interface

2013-05-07 Thread Christian Stoller
> On Tue, May 7, 2013 at 6:17 PM, Thomas Anderson  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 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


Re: [PHP-DEV] idea: implement a Comparable interface

2013-05-07 Thread Daniel Ribeiro
To me, it sounds that those extremely specific cases ask for a extremely
specific compareTo(stdClass) method.


Daniel Ribeiro Gomes Pereira
Twitter  |
Facebook
 | LinkedIn 
iPhone: +55 (48) 9111-0931


2013/5/7 Stas Malyshev 

> 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
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Re: [PHP-DEV] idea: implement a Comparable interface

2013-05-07 Thread Stas Malyshev
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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] idea: implement a Comparable interface

2013-05-07 Thread Stas Malyshev
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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] idea: implement a Comparable interface

2013-05-07 Thread Thomas Anderson
On Tue, May 7, 2013 at 1:05 PM, Nikita Popov  wrote:

> On Tue, May 7, 2013 at 6:17 PM, Thomas Anderson  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.
>

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.


Re: [PHP-DEV] idea: implement a Comparable interface

2013-05-07 Thread Jake Bell
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  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  wrote:
> 
>> Its kinda useless feature for PHP.
>> 
>> 
>> Daniel Ribeiro Gomes Pereira
>> Twitter  |
>> Facebook
>> | LinkedIn 
>> iPhone: +55 (48) 9111-0931
>> 
>> 
>> 2013/5/7 Nikita Popov 
>> 
>>> On Tue, May 7, 2013 at 6:17 PM, Thomas Anderson 
>> 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
>>> 
>> 


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] idea: implement a Comparable interface

2013-05-07 Thread Stuart Langley
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  wrote:

> Its kinda useless feature for PHP.
>
>
> Daniel Ribeiro Gomes Pereira
> Twitter  |
> Facebook
>  | LinkedIn 
> iPhone: +55 (48) 9111-0931
>
>
> 2013/5/7 Nikita Popov 
>
> > On Tue, May 7, 2013 at 6:17 PM, Thomas Anderson 
> 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
> >
>


Re: [PHP-DEV] idea: implement a Comparable interface

2013-05-07 Thread Daniel Ribeiro
Its kinda useless feature for PHP.


Daniel Ribeiro Gomes Pereira
Twitter  |
Facebook
 | LinkedIn 
iPhone: +55 (48) 9111-0931


2013/5/7 Nikita Popov 

> On Tue, May 7, 2013 at 6:17 PM, Thomas Anderson  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
>


Re: [PHP-DEV] idea: implement a Comparable interface

2013-05-07 Thread Nikita Popov
On Tue, May 7, 2013 at 6:17 PM, Thomas Anderson  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


Re: [PHP-DEV] idea: implement a Comparable interface

2013-05-07 Thread Adam Harvey
On 7 May 2013 09:17, Thomas Anderson  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.

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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] idea: implement a Comparable interface

2013-05-07 Thread Levi Morrison
> 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.


Re: [PHP-DEV] idea: implement a Comparable interface

2013-05-07 Thread Levi Morrison
> 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.