Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:49821 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 69890 invoked from network); 30 Sep 2010 15:26:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Sep 2010 15:26:03 -0000 Authentication-Results: pb1.pair.com smtp.mail=adam@adamharvey.name; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=adam@adamharvey.name; sender-id=pass Received-SPF: pass (pb1.pair.com: domain adamharvey.name designates 209.85.214.170 as permitted sender) X-PHP-List-Original-Sender: adam@adamharvey.name X-Host-Fingerprint: 209.85.214.170 mail-iw0-f170.google.com Received: from [209.85.214.170] ([209.85.214.170:55532] helo=mail-iw0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 14/00-04170-60CA4AC4 for ; Thu, 30 Sep 2010 11:25:59 -0400 Received: by iwn37 with SMTP id 37so3000431iwn.29 for ; Thu, 30 Sep 2010 08:25:56 -0700 (PDT) Received: by 10.231.157.195 with SMTP id c3mr3896494ibx.155.1285860355612; Thu, 30 Sep 2010 08:25:55 -0700 (PDT) MIME-Version: 1.0 Sender: adam@adamharvey.name Received: by 10.231.79.3 with HTTP; Thu, 30 Sep 2010 08:25:28 -0700 (PDT) In-Reply-To: References: Date: Thu, 30 Sep 2010 23:25:28 +0800 X-Google-Sender-Auth: 5AdK8KbHMNamQK4z0byHBl1nPY8 Message-ID: To: Gustavo Lopes Cc: internals@lists.php.net Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] RFC: Comparable interface From: aharvey@php.net (Adam Harvey) On 30 September 2010 23:13, Gustavo Lopes wrote: > That is, you would only modify zend_std_compare_objects (not > compare_function!) and add there the additional logic where you'd check if > the objects had the same class (or perhaps one was of a subclass of the > other, in which case you'd use the compare method of the subclass) and call > the compare method. > > Allowing comparison of objects with non-objects can become very confusing: > > * What if the objects are not of the same class? What if one is of a > subclass of the other? Is equality not symmetric? > * How do you interact with 'get' and 'cast_object'/'__tostring'? The initial version of the patch actually did just that: it simply modified zend_std_compare_objects. That's pretty much the cleanest way you could do it. :) I dropped it and went with the more complicated approach involving changing compare_function because I actually think allowing comparison with non-objects is a valuable feature. Objects may be more strongly typed in PHP than scalars, but there's still a certain amount of juggling that can take place via __toString(), so I think it would be out of place to require an object on both sides. Additionally, there are already semantics built into compare_function to handle object/non-object comparisons, although they're of very limited use in practice. The specific use case I had in mind was allowing comparisons of objects representing monetary amounts against numeric values (since that was a particular itch I had a couple of years ago), but I'm sure there would be others. In terms of the specific question as to whether equality is symmetric: certainly it should be, but we would have no way of enforcing it, obviously. Java doesn't either, though: there's simply a note in the documentation saying that implementations should be symmetric. Adam