note 98563 deleted from function.spl-object-hash by danbrown
| From: | danbrown@php.net | Date: | Fri, 25 Jun 2010 15:15:48 +0000 |
| Subject: | note 98563 deleted from function.spl-object-hash by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170440@lists.php.net to get a copy of this message | ||
Note Submitter: aercolino at yahoo dot com
----
For PHP < 5.2, Salvioni's code (see below) only captures the last digit and has unnecessary
steps. This is the code I came up with:
<?php
if (!function_exists('spl_object_hash')) {
/**
* Returns an hash that uniquely identifies the given $object
*
* @param object $object
* @return string
*/
function spl_object_hash( $object )
{
if (! is_object($object))
{
trigger_error(__FUNCTION__ . "() expects parameter 1 to be object",
E_USER_WARNING);
return null;
}
ob_start();
var_dump($object);
$dump = ob_get_clean();
preg_match('/^object\([^)]+\)\#\d+/i', $dump, $matches);
$result = md5($matches[0]); //$matches[0] is something like "object(MyClass)#12"
return $result;
}
}
?>