Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:77592 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 85403 invoked from network); 24 Sep 2014 20:08:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Sep 2014 20:08:51 -0000 Authentication-Results: pb1.pair.com header.from=smalyshev@sugarcrm.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=smalyshev@sugarcrm.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain sugarcrm.com designates 108.166.43.67 as permitted sender) X-PHP-List-Original-Sender: smalyshev@sugarcrm.com X-Host-Fingerprint: 108.166.43.67 smtp67.ord1c.emailsrvr.com Linux 2.6 Received: from [108.166.43.67] ([108.166.43.67:52492] helo=smtp67.ord1c.emailsrvr.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 46/D4-57585-2D423245 for ; Wed, 24 Sep 2014 16:08:50 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp25.relay.ord1c.emailsrvr.com (SMTP Server) with ESMTP id 2E0A31803F1; Wed, 24 Sep 2014 16:08:48 -0400 (EDT) X-Virus-Scanned: OK Received: by smtp25.relay.ord1c.emailsrvr.com (Authenticated sender: smalyshev-AT-sugarcrm.com) with ESMTPSA id B4E46180387; Wed, 24 Sep 2014 16:08:47 -0400 (EDT) X-Sender-Id: smalyshev@sugarcrm.com Received: from Stass-MacBook-Pro.local ([UNAVAILABLE]. [74.85.23.222]) (using TLSv1.2 with cipher DHE-RSA-AES128-SHA) by 0.0.0.0:465 (trex/5.2.13); Wed, 24 Sep 2014 20:08:48 GMT Message-ID: <542324D0.6050407@sugarcrm.com> Date: Wed, 24 Sep 2014 13:08:48 -0700 Organization: SugarCRM User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:31.0) Gecko/20100101 Thunderbird/31.1.1 MIME-Version: 1.0 To: Andrea Faulds CC: =?UTF-8?B?Sm9oYW5uZXMgU2NobMO8dGVy?= , Nicolai Scheer , PHP Internals References: <1411561476.2303.4.camel@guybrush> <54232205.4020806@sugarcrm.com> <3E4C58C8-E816-4798-9D8F-2C7EFFA28438@ajf.me> In-Reply-To: <3E4C58C8-E816-4798-9D8F-2C7EFFA28438@ajf.me> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Invokation on __toString() for object used as array key From: smalyshev@sugarcrm.com (Stas Malyshev) Hi! > I’m not sure that’d make much sense. The object isn’t the key, the > value the magic method returns is. It would be quite odd to do this: > > $someArray = [$my__hashImplementingObject => 1]; > var_dump($someArray); > > And see something like this, because we’ve called a hash function: > > array(1) { ["ec10e5a66e281d105f302cacfb1aaca8"]=> int(0) } The hash doesn't have to be a nonsensical hex value, it can be something like "My Object Number 42" if you want to. The difference is that __toString is for human reading, and it's not always suitable for hashing purposes. Just read the docs on any of the languages that have separate hash method - they all have the same argument, there's a different between printing an object for output and using the object as a key. > I don’t really see what advantage this has over the normal > __toString. Furthermore, having a special method we use to cast here > that’s used nowhere else seems weird. That's the point - it's not a cast. It's an operation that requires object's identity. Again, given that so many languages have it, I don't think it's really that weird. I think it's pretty natural. > Now, if we were to add actual object key support, that I might like. > But if we’re going to keep with just integers and strings, I’d 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. The problem is you mix here two completely different domains. On one hand, you may want the object to have text representation for output purposes - say, XML object would have XML output as it's string rep. On other hand, I don't think you want to use 100K XML as a key, because using as a key is a completely different issue. Converting to string is a hack that mixes two different problems into one method, and that's why it will lead to problems. The solution for this problem is known and widely used - have a separate hash method. With PHP, you can actually have a luxury of using a human-readable keys while still keeping them nice and clean and independent from string representation. And if you want maximum efficiency, you could switch to ints instead. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/