(PHP 5 >= 5.2.0)
spl_object_hash — Return hash id for given object
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.
Any object.
A string that is unique for each currently existing object and is always the same for each object.
Beispiel #1 A spl_object_hash() example
<?php
$id = spl_object_hash($object);
$storage[$id] = $object;
?>
Hinweis:
When an object is destroyed, its hash may be reused for other objects.
To add to markus dot albe at mailinator dot com comment.
The unique identifiers of destroyed objects can and will be reused.
<?php
class Foo{
private static $test = 0;
public function __construct(){
self::$test++;
}
}
$foo1 = new Foo();
$foo2 = new Foo();
$foo3 = new Foo();
//new unique identifier
echo spl_object_hash($foo1)."<br />\n";
//new unique identifier
echo spl_object_hash($foo2)."<br />\n";
//new unique identifier
echo spl_object_hash($foo3)."<br />\n";
$foo1 = new Foo();
//new unique identifier because, when we created Foo, foo1 still existed
echo spl_object_hash($foo1)."<br />\n";
$foo1 = new Foo();
//same identifier as the first $foo1 because it was destroyed,
//thus released the identifier
echo spl_object_hash($foo1)."<br />\n";
?>
This will output something like:
000000007ec3fcb5000000003f984510
000000007ec3fcb6000000003f984510
000000007ec3fcb7000000003f984510
000000007ec3fcb0000000003f984510
000000007ec3fcb5000000003f984510
It seems that when switching scopes, the last one is repeated... Not necessarily a bug, but could create some problems/unexpected behavior
<?php
$var = new stdClass();
var_dump(spl_object_hash($var), spl_object_hash(new stdClass()));
hola();
function hola() {
$var = new stdClass();
var_dump(spl_object_hash($var), spl_object_hash(new stdClass()));
}
/* will output something like
string(32) "f4c9a9e4c63bb94a344b09fa90b57d5c"
string(32) "7b6e43337944619e217ded14a7a78d35"
string(32) "7b6e43337944619e217ded14a7a78d35"
string(32) "ebd1bf725146297194bb011ef5e9c4d5"
*/
?>
It is possible to mark objects with unique identity to establish required functionality:
<?php
if (!function_exists('spl_object_hash')) {
function spl_object_hash($object) {
if (!is_object($object)) {
trigger_error(__FUNCTION__ . "() expects parameter 1 to be object", E_USER_WARNING);
return null;
}
if (!isset($object->__oid__))
$object->__oid__ = uniqid();
}
return $object->__oid__;
}
}
?>
With regards to the previous post, I'm not entirely sure that easy does do it. Consider the following code:
<?php
class Testy
{
public $something;
public function __toString()
{
return serialize($this);
}
}
$objOne = new Testy();
$objOne->something = "hello";
$objTwo = new Testy();
$objTwo->something = "hello";
printf("objOne:\n");
printf("Bad way: %s\n", md5((string)$objOne));
printf("SPL Way: %s\n", spl_object_hash($objOne));
printf("objTwo:\n");
printf("Bad way: %s\n", md5((string)$objTwo));
printf("SPL Way: %s\n", spl_object_hash($objTwo));
?>
The spl_object_hash function returns a hash based on the particular object, not the content. The spl hash will always be the same for a given object, regardless of content.
Easy does it
<?php
if (!function_exists('spl_object_hash')) {
/**
* Returns the hash of the unique identifier for the object.
*
* @param object $object
* @author Andreas Rein
* @return string
*/
function spl_object_hash($object) {
if (is_object($object)) {
return md5((string)$object);
}
trigger_error(__FUNCTION__ . "() expects parameter 1 to be object", E_USER_WARNING);
return null;
}
}
?>
The follow function is a implementation of the PHP´s function spl_object_hash(), unavailable in versions less 5.2.0.
But, the algorithm of this function is different of the original PHP´s function.
(Sorry... my english is very bad...)
<?php
if (!function_exists('spl_object_hash')) {
/**
* Returns the hash of the unique identifier for the object.
*
* @param object $object Object
* @author Rafael M. Salvioni
* @return string
*/
function spl_object_hash($object)
{
if (is_object($object)) {
ob_start(); var_dump($object); $dump = ob_get_contents(); ob_end_clean();
if (preg_match('/^object\(([a-z0-9_]+)\)\#(\d)+/i', $dump, $match)) {
return md5($match[1] . $match[2]);
}
}
trigger_error(__FUNCTION__ . "() expects parameter 1 to be object", E_USER_WARNING);
return null;
}
}
?>
Note that the contents (properties) of the object are NOT hashed by the function, merely its internal handle and handler table pointer. 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, for example:
var_dump(spl_object_hash(new stdClass()), spl_object_hash(new stdClass()));
Running this alone will usually generate the same hashes, since PHP reuses the internal handle for the first stdClass after it has been dereferenced and destroyed when it creates the second stdClass.