Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:77591 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 83472 invoked from network); 24 Sep 2014 20:01:19 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Sep 2014 20:01:19 -0000 Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.216 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.216 imap10-3.ox.privateemail.com Received: from [192.64.116.216] ([192.64.116.216:56009] helo=imap10-3.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FD/64-57585-E0323245 for ; Wed, 24 Sep 2014 16:01:19 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 6FCAC2400C7; Wed, 24 Sep 2014 16:01:15 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at imap10.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap10.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id ZKKXE22K7fPK; Wed, 24 Sep 2014 16:01:15 -0400 (EDT) Received: from oa-res-27-90.wireless.abdn.ac.uk (oa-res-27-90.wireless.abdn.ac.uk [137.50.27.90]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id E5B642400DD; Wed, 24 Sep 2014 16:01:12 -0400 (EDT) Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) In-Reply-To: <54232205.4020806@sugarcrm.com> Date: Wed, 24 Sep 2014 21:01:09 +0100 Cc: =?windows-1252?Q?Johannes_Schl=FCter?= , Nicolai Scheer , PHP Internals Content-Transfer-Encoding: quoted-printable Message-ID: <3E4C58C8-E816-4798-9D8F-2C7EFFA28438@ajf.me> References: <1411561476.2303.4.camel@guybrush> <54232205.4020806@sugarcrm.com> To: Stas Malyshev X-Mailer: Apple Mail (2.1878.6) Subject: Re: [PHP-DEV] Invokation on __toString() for object used as array key From: ajf@ajf.me (Andrea Faulds) On 24 Sep 2014, at 20:56, Stas Malyshev wrote: >> There the assumption would be that this leads to an array $a with two >> elements, where in fact there is only one if __toString() is being >> called. The only thing "making sense" would be using using the = objects >> identity (i.e. via spl_object_hash()) everything else leads to = issues. >=20 > This is a valid concern. For this, Java, for example, has separate > methods hashCode() and toString(). Python has __str__, __repr__ and > __hash__. Ruby has object.hash. So maybe we should have another magic, > something like __hash(), that would produce a value for key? Then > objects that implement __hash would be hashable and those that don't > won't be, while still having usable __toString. I=92m not sure that=92d make much sense. The object isn=92t the key, the = value the magic method returns is. It would be quite odd to do this: $someArray =3D [$my__hashImplementingObject =3D> 1]; var_dump($someArray); And see something like this, because we=92ve called a hash function: array(1) { ["ec10e5a66e281d105f302cacfb1aaca8"]=3D> int(0) } I don=92t really see what advantage this has over the normal __toString. = Furthermore, having a special method we use to cast here that=92s used = nowhere else seems weird. Now, if we were to add actual object key support, that I might like. But = if we=92re going to keep with just integers and strings, I=92d much = prefer to just support __toString here. I think users are smart enough = to understand that PHP arrays only have string or int keys, so it casts = to a string. -- Andrea Faulds http://ajf.me/