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);   
}

[EMAIL PROTECTED] /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);
}

[EMAIL PROTECTED] /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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to