Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:23888 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 14087 invoked by uid 1010); 3 Jun 2006 16:36:44 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 14072 invoked from network); 3 Jun 2006 16:36:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Jun 2006 16:36:44 -0000 X-PHP-List-Original-Sender: helly@php.net X-Host-Fingerprint: 81.169.182.136 ajaxatwork.net Linux 2.4/2.6 Received: from ([81.169.182.136:59381] helo=strato.aixcept.de) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id 6D/50-22639-B9AB1844 for ; Sat, 03 Jun 2006 12:36:44 -0400 Received: from baumbart.mbo (dslb-084-063-007-047.pools.arcor-ip.net [84.63.7.47]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by strato.aixcept.de (Postfix) with ESMTP id 6AAE235C1C1; Sat, 3 Jun 2006 18:36:40 +0200 (CEST) Date: Sat, 3 Jun 2006 18:38:59 +0200 Reply-To: Marcus Boerger X-Priority: 3 (Normal) Message-ID: <509342741.20060603183859@marcus-boerger.de> To: Andrew Yochum Cc: internals@lists.php.net, Dmitry Stogov , Andi Gutmans , Zeev Suraski , Ilia Alshanetsky In-Reply-To: <20060603143639.GF5355@desario.homelinux.net> References: <795156743.20060603134212@marcus-boerger.de> <20060603143639.GF5355@desario.homelinux.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Missing __toString() part From: helly@php.net (Marcus Boerger) Hello Andrew, Saturday, June 3, 2006, 4:36:39 PM, you wrote: > On Sat, Jun 03, 2006 at 01:42:12PM +0200, Marcus Boerger wrote: >> Hello guys, >> >> the attached patch closes one more __toString() part. It allows >> to use objects that define __toString as indexes to arrays. What do >> you guys think about this, should we add it or stay with the old >> behavior that didn't allow objects as indexes at all. > Like others who have contributed to the thread I agree that it would be > useful, but I think using __toString() wrong for this. There is a > confusion two ideas here: > 1. The string representation of an object, probably human > readable, lucent string. > 2. A unique hash of an object use to identify it programmatically, > and likely unique. Probably a non-human friendly, opaque value. > These are not the same thing. The latter is what, IMO, would be used > for an objects value as an index into an array, not the former. > The latter is available ... currently I do this like so: > class a{ public function __toString() { return 'an object';}} > $foo = new a; > $bar = new a; > $arr = new array() > $arr[(string)$foo] = $foo; // The index is "Object id #1" > $arr[(string)$bar] = $bar; // The index is "Object id #2" > Note that the __tostring method is not used in the cast - a spearate > issue, but needs to be pointed out for this issue. > Therefore my suggested alternative to Marcus' patch is: > When Objects are used in array index context, use the object ID or some > variation. And, possibly, provide a magic method, or interface to > generate an hash value of an object. We cannot use the object id since that is not necessarily unique. For the same reason SplObjectStorage cannot be done in userland. However what you mention here is right. Other languages offer to magic functions. A stringify one and a hashing one. But then again i don't think we should add yet another magic feature. best regards marcus