note 129141 added to function.spl-object-hash
| From: | Numety at main dot php dot net | Date: | Thu, 04 Jan 2024 10:36:49 +0000 |
| Subject: | note 129141 added to function.spl-object-hash | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221095@lists.php.net to get a copy of this message | ||
As others have noted, this function now returns the ID, padded with zeroes.
It does not produce a cryptographic hash (which is not what the name hints at, either), nor does it
hide it which order the objects were created.
If you wish to give your objects unique identifiers while hiding in which order they were created,
you can achieve this by hashing their ID together with other predictable values:
<?php
function objectHash(object $object): string
{
return hash('sha512', $object::class . spl_object_id($object));
}
?>
Here's an example usage:
<?php
class Person
{
function __construct(public string $name) {}
}
$anna = new Person('Anna');
$bob = new Person('Bob');
var_export(objectHash($anna)); // '077d33c1 etc.
echo "\n";
var_export(objectHash($bob)); // 'ea3c1319 etc.
?>
Feel free to choose another hash, or hash other values along with their ID, for it to better suit
your environment.
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 2a02:a449:c7a9:0:60eb:5c69:e2:30de)
----
Manual Page -- https://php.net/manual/en/function.spl-object-hash.php
Edit -- https://main.php.net/note/edit/129141
Del: integrated -- https://main.php.net/note/delete/129141/integrated
Del: useless -- https://main.php.net/note/delete/129141/useless
Del: bad code -- https://main.php.net/note/delete/129141/bad+code
Del: spam -- https://main.php.net/note/delete/129141/spam
Del: non-english -- https://main.php.net/note/delete/129141/non-english
Del: in docs -- https://main.php.net/note/delete/129141/in+docs
Del: other reasons-- https://main.php.net/note/delete/129141
Reject -- https://main.php.net/note/reject/129141
Search -- https://main.php.net/manage/user-notes.php