Voting

: five minus five?
(Example: nine)

The Note You're Voting On

Numety
1 year ago
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.

<< Back to user notes page

To Top