Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:77594 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 89752 invoked from network); 24 Sep 2014 20:47:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Sep 2014 20:47:09 -0000 Authentication-Results: pb1.pair.com header.from=johannes@schlueters.de; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=johannes@schlueters.de; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain schlueters.de from 217.114.215.10 cause and error) X-PHP-List-Original-Sender: johannes@schlueters.de X-Host-Fingerprint: 217.114.215.10 mail.experimentalworks.net Received: from [217.114.215.10] ([217.114.215.10:48657] helo=mail.experimentalworks.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 91/A5-57585-CCD23245 for ; Wed, 24 Sep 2014 16:47:09 -0400 Received: by mail.experimentalworks.net (Postfix, from userid 1003) id 8370D42445; Wed, 24 Sep 2014 22:47:16 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on km31408.keymachine.de X-Spam-Level: X-Spam-Status: No, score=-1.0 required=3.0 tests=ALL_TRUSTED autolearn=unavailable version=3.3.2 X-Spam-HAM-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP Received: from [192.168.2.34] (ppp-93-104-15-0.dynamic.mnet-online.de [93.104.15.0]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: johannes@schlueters.de) by mail.experimentalworks.net (Postfix) with ESMTPSA id 614D942421; Wed, 24 Sep 2014 22:47:12 +0200 (CEST) Message-ID: <1411591596.14092.9.camel@kuechenschabe> To: Andrea Faulds Cc: Nicolai Scheer , PHP Internals Date: Wed, 24 Sep 2014 22:46:36 +0200 In-Reply-To: References: <1411561476.2303.4.camel@guybrush> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.10.4-0ubuntu2 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Invokation on __toString() for object used as array key From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) On Wed, 2014-09-24 at 20:45 +0100, Andrea Faulds wrote: > On 24 Sep 2014, at 13:24, Johannes Schlüter wrote: > > > Taking this sample code::: > > > > > class C { > > function __toString() { > > return "C"; > > } > > } > > > > $a = []; > > > > $c1 = new C(); > > $c2 = new C(); > > > > $a[$c1] = 23; > > $a[$c2] = 42; > > ?> > > > > 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. > > I’m not sure this is a fair example. Don’t classes usually implement a > `__toString()` that is based on the data they contain? If they don’t, > I’m not sure they’re useful for indexing anyway. "Usually" is a bad term - we have no idea what >99% of our suers do ;) I don't think there is a clear leader on usage of __toString(), for some it is a debugging feature, for some a "normal" operation. The interesting question are value objects We can define we're using object identity as I did above or we add a __hash() method, which again is more magic. Last time this was discussed the magic didn't win and therefore having objects as keys is forbidden so the user has to decide on hashing/identifying himself in an explicit way. johannes