spl_object_hash is not unique

Here is why you should avoid using spl_object_hash() in PHP

from the documentation: "This function returns a unique identifier for the object. This id can be used as a hash key for storing objects or for identifying an object."

spl_object_hash() actually returns an md5 hash of the internal pointer of an object.

PHP efficiently reuses internal pointers; meaning the second you remove an object the pointer is reused.

thus:

<?php
$transaction_ids = array();
for( $i=0; $i<10; $i++ ) {
 $transaction = (object)('transaction ' . $i);
 $hash = spl_object_hash( $transaction );
 $transaction_ids[] = $hash;
}
print_r( $transaction_ids );
?>

will output:

Array
(
[0] => 4b8051a089b9e8352a97af234d4478a7
[1] => 076b44c4ffe009b98c0ba3348459c57a
[2] => 4b8051a089b9e8352a97af234d4478a7
[3] => 076b44c4ffe009b98c0ba3348459c57a
[4] => 4b8051a089b9e8352a97af234d4478a7
[5] => 076b44c4ffe009b98c0ba3348459c57a
[6] => 4b8051a089b9e8352a97af234d4478a7
[7] => 076b44c4ffe009b98c0ba3348459c57a
[8] => 4b8051a089b9e8352a97af234d4478a7
[9] => 076b44c4ffe009b98c0ba3348459c57a
)

not even remotely unique, could lead to serious problems, and simply.. I'd forget using it if I was you.

sucks

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Printed from: http://webr3.org/blog/php/spl_object_hash-is-not-unique/ .
© Your Name Here 2010.

1 Comment   »

RSS feed for comments on this post , TrackBack URI

Leave a Reply

Additional comments powered by BackType

  • webr3 avatar