Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:23912 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 74206 invoked by uid 1010); 4 Jun 2006 10:06:36 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 74191 invoked from network); 4 Jun 2006 10:06:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Jun 2006 10:06:36 -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:60778] helo=strato.aixcept.de) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id EE/60-49656-BA0B2844 for ; Sun, 04 Jun 2006 06:06:35 -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 4FF4635C1EA; Sun, 4 Jun 2006 12:06:31 +0200 (CEST) Date: Sun, 4 Jun 2006 12:08:52 +0200 Reply-To: Marcus Boerger X-Priority: 3 (Normal) Message-ID: <64299052.20060604120852@marcus-boerger.de> To: Rasmus Lerdorf , Andi Gutmans Cc: internals@lists.php.net In-Reply-To: <44823C51.7040408@lerdorf.com> References: <795156743.20060603134212@marcus-boerger.de> <20060603143639.GF5355@desario.homelinux.net> <509342741.20060603183859@marcus-boerger.de> <7.0.1.0.2.20060603175211.02208a50@zend.com> <20060604030100.1093d2f9@pierre-u64> <7.0.1.0.2.20060603181129.0396fc18@zend.com> <44823B41.5000608@akbkhome.com> <44823C51.7040408@lerdorf.com> 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 Rasmus, Andi, first to Andi, there is no BC involved beside the fact that this would allow objects being used in arrays which wasn't possible before. Now a hashing value can easily be generated: char * hash; int len = spprintf(&hash,0,"%p:%d",Z_OBJ_HT_P(zobj),Z_OBJ_HANDLE_P(zobj)); That said maybe it is enough to add a generic hash function either the engine or SPL: /* {{{ proto string spl_object_hash(object obj) Return hash id for given object */ PHP_FUNCTION(spl_object_hash) { zval *obj; int len; char *hash; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) { return; } len = spprintf(&hash, 0, "%p:%d", Z_OBJ_HT_P(obj), Z_OBJ_HANDLE_P(obj)); RETURN_STRINGL(hash, len, 0); } marcus@zaphod /usr/src/PHP_5_2 $ php -r 'var_dump(spl_object_hash(new stdClass));' string(11) "0x87b6a40:1" Maybe we want to scramble this with md5 or something alike: /* {{{ proto string spl_object_hash(object obj) Return hash id for given object */ PHP_FUNCTION(spl_object_hash) { zval *obj; int len; char *hash; char md5str[33]; PHP_MD5_CTX context; unsigned char digest[16]; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) { return; } len = spprintf(&hash, 0, "%p:%d", Z_OBJ_HT_P(obj), Z_OBJ_HANDLE_P(obj)); md5str[0] = '\0'; PHP_MD5Init(&context); PHP_MD5Update(&context, (unsigned char*)hash, len); PHP_MD5Final(digest, &context); make_digest(md5str, digest); RETVAL_STRING(md5str, 1); efree(hash); } marcus@zaphod /usr/src/PHP_5_2 $ php -r 'var_dump(spl_object_hash(new stdClass));' string(32) "0bab1548e3b42acbcf1170617b5432ae" For PHP 5.2 we could think about adding a hash handler to the object handler table and provide the above as default implementation. That hash handler would then be called when an object is being used as an offset. Maybe later in PHP 6 we can also add an interface that allows to overload that hash implementation. (If someone really wants that). Yet anything the user can overload be it an interface or magic function (e.g. __hashOf) seems to be too much for now. Besides that i am meanwhile convinced that __toString shouldn't have to do anything with objects as array offsets other then it can be requested by the user thorugh a string cast (e.g.: $ar[(string)$obj]). best regards marcus Sunday, June 4, 2006, 3:50:09 AM, you wrote: > Alan Knowles wrote: >> >> $x = new Obj; >> $y[$x]= 123; >> >> That behaviour is going to be fun to document, and for people unfamilar >> with it to find in the manual >> -> php.net/array -> go to array syntax page -> read down to find about >> objects as keys -> go to __toHash() page... >> >> ..whereas... >> $x = new Obj; >> $y[$x->toHash()]= 123; >> >> they might have a chance to see instantly how that is supposed to work, >> on the rare occasion that somebody needs this, is it really worth >> building it into the langauge???? > In that case, why not just use the existing __toString explicitly with: > $x = new Obj; > $y[(string)$x] = 123; > This works today and basically gives you the same thing. The only thing > it doesn't do is separate an array index context from an output context > allowing you do different things in the two cases. > I think if we are going to add a toHash thing, it should trigger when > the object is used in a hash context, just like toString triggers when > it is used in a string context. > -Rasmus Best regards, Marcus