Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:83283 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 47423 invoked from network); 20 Feb 2015 10:05:22 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Feb 2015 10:05:22 -0000 Authentication-Results: pb1.pair.com smtp.mail=dennis@birkholz.biz; spf=unknown; sender-id=unknown Authentication-Results: pb1.pair.com header.from=dennis@birkholz.biz; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain birkholz.biz does not designate 144.76.185.252 as permitted sender) X-PHP-List-Original-Sender: dennis@birkholz.biz X-Host-Fingerprint: 144.76.185.252 mx01.nexxes.net Received: from [144.76.185.252] ([144.76.185.252:39628] helo=mx01.nexxes.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 28/F9-25547-5A607E45 for ; Fri, 20 Feb 2015 05:04:23 -0500 Received: from [137.226.183.192] (ip3192.saw.rwth-aachen.de [137.226.183.192]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: db220660-p0g-1@packages.nexxes.net) by mx01.nexxes.net (Postfix) with ESMTPSA id A6621480B37 for ; Fri, 20 Feb 2015 11:04:18 +0100 (CET) Message-ID: <54E706A2.4050808@birkholz.biz> Date: Fri, 20 Feb 2015 11:04:18 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Comparable: the revenge From: dennis@birkholz.biz (Dennis Birkholz) Hi Adam, Am 19.02.2015 um 22:40 schrieb Adam Harvey: > RFC: https://wiki.php.net/rfc/comparable > PR: https://github.com/php/php-src/pull/1097 I see a little problem here by allowing any comparison. Comparing is used for ordering, but you can easily construct ordering mechanisms that simply don't work: class A implements Comparable { public $value; function compareTo($other) { if ($other instanceof A) { return $this->value <=> $other->value; } else { return -1; } } } class B implements Comparable { public $value; function compareTo($other) { if ($other instanceof B) { return $this->value <=> $other->value; } else { return -1; } } } $a = new A(); $a->value = 1; $b = new B(); $b->value = 2; $a->compareTo($b); // -1 $b->compareTo($a); // -1 If you now have a mixed list of objects of A and B, the actual order in the list completely depends whether A's compareTo() method is called or B's compareTo() method. I think comparison should always be symmetric: $a < $b <=> $b > $a Otherwise the behavior is nearly unpredictable/random. As we have no method overloading, the only viable option I see is to only allow comparison of objects of the same type. Thanks Dennis