Nick Cooper schreef:
> I am having a problem with spl_object_hash() creating non unique hashes.
> 
> I understand with MD5 it is possible to have the same hash for
> different strings but this doesn't seem like that problem.
> 
> I have created a simple test below, should I report this as a bug or
> am I doing something wrong.

your making an incorrect assumption about spl_object_hash(),
using unset($obi) tells the engine that there are no more
references to the given object that was stored in it ... this
implies the relevant internal object pointer is free to be reused.

> PHP 5.2.8 (extensions all disabled)
> Win XP SP3
> Apache 2.2.11
> 
> <?php
> class a1 {}
> $obi = new a1();
> echo get_class($obi).': '.spl_object_hash($obi).'<br/>';
> class a2 {}
> $obi = new a2();
> echo get_class($obi).': '.spl_object_hash($obi).'<br/>';
> class a3 {}
> $obi = new a3();
> echo get_class($obi).': '.spl_object_hash($obi).'<br/>';
> unset($obi);
> class a4 {}
> $obi = new a4();
> echo get_class($obi).': '.spl_object_hash($obi).'<br/>';
> ?>
> 
> Outputs:
> 
> a1: 09d264fcececf51c822c9382b40e3edf
> a2: 45701af64172cbc2a33069dfed73fd07
> a3: 09d264fcececf51c822c9382b40e3edf
> a4: 09d264fcececf51c822c9382b40e3edf
> 
> Thanks let me know how I should proceed with this.

to quote the user notes in the manual:

"This is sufficient to guarantee that any two objects simultaneously 
co-residing in memory will have different hashes.
Uniqueness is not guaranteed between objects that did not reside in memory 
simultaneously,"

note also that the hash given is only valid for the life of the request, the 
hash is not unique accross concurrent
requests and the hash value is dependent on the order the objects were created.

php -r 'class Test {}; $t1 = new Test; $t2 = new Test; 
var_dump(spl_object_hash($t1), spl_object_hash($t2));'
php -r 'class Test {}; $t1 = new Test; $t2 = new Test; 
var_dump(spl_object_hash($t2), spl_object_hash($t1));'
php -r 'class Test {}; $t2 = new Test; $t1 = new Test; 
var_dump(spl_object_hash($t1), spl_object_hash($t2));'
php -r 'class Test {}; $t0 = new Test; $t1 = new Test; $t2 = new Test; 
var_dump(spl_object_hash($t1), spl_object_hash($t2));'

use something else if you need unique hashes for objects not
simultaneously existing.

> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to