On 3 August 2015 08:20:18 BST, Nicolas Grekas <nicolas.grekas+...@gmail.com> 
wrote:
>2015-08-02 20:03 GMT+02:00 Rowan Collins <rowan.coll...@gmail.com>:
>
>> On 02/08/2015 18:41, Bob Weinand wrote:
>>
>>> Some suspicious use of spl_object_hash() out there...
>>>> >
>>>>
>>>>> >>
>>>>>
>https://github.com/symfony/symfony/blob/master/src/Symfony/Component/VarDumper/Cloner/VarCloner.php
>>>>>
>>>> >
>>>> >Not sure what this one does... but calculations with
>spl_object_hash()
>>>> >look very suspicious.
>>>>
>>> Actually, it's doing the right thing… calculating the value the
>object id
>>> is xor'ed with (as we know that consecutively defined objects have
>>> consecutive ids).
>>> It's relying on the implementation of spl_object_hash() and will
>even
>>> continue to work when we remove that part of randomness as that
>value it's
>>> xor'ed with is then nothing else than 0.
>>>
>>
>> The right thing for what purpose? Why does it need that ID, rather
>than
>> the value that spl_object_hash() gave in the first place? Just to be
>> prettier to the user?
>>
>
>
>For the purpose of providing an id that humans can read and compare, 

So, yes, just to make it more human friendly. In which case, the actual value 
doesn't matter, and rather than reverse-engineering the hash, you could just 
make up your own ID:

function my_object_id($obj) {
    static $hash_to_id=[];
    $hash = spl_object_hash($obj);
    if ( ! array_key_exists($hash, $hash_to_id) ) {
        $hash_to_id[$hash] = count( $hash_to_id);
    }
    return $hash_to_id[$hash];
}

(Untested, but pretty much all you need.)

Regards,
-- 
Rowan Collins
[IMSoP]


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

Reply via email to