spl_object_hash

Returns a unique string identifier for an object, usable as an array key while the object is alive.

Signature
spl_object_hash(object $object): string
  • Added in PHP 5.2.0

Parameters

ParameterTypeDescription
$objectobjectAny object.

Return value

stringA 32-character hexadecimal string that is unique among all objects currently in memory.

Errors

Throws a TypeError if the argument is not an object.

Changelog

  • PHP 8.1.0The returned string is now the object's id rendered as a zero-padded hexadecimal value rather than an opaque hash.

spl_object_hash() gives each live object a string you can use as an array key, since PHP arrays cannot be keyed by objects directly. Two distinct objects alive at the same time always get different strings, and the same object always gets the same one.

The word "hash" is misleading: it is derived from the object's internal handle, not its contents. Two objects with identical property values get different hashes; the same object mutated beyond recognition keeps its hash. It is an identity, never a checksum — do not use it for caching by value or for equality comparisons of data.

When to reach for it

  • Detecting cycles while walking an object graph — mark each visited node in a keyed array.
  • Building a lightweight object-to-metadata map when you do not want an SplObjectStorage.

What trips people up

The identifier is only unique among objects that currently exist. Once an object is garbage collected, its handle — and therefore its hash — can be handed to the next object created. A long-lived array of hashes for objects you no longer reference will silently start colliding. Keep a reference to the object alongside its hash, or use SplObjectStorage, which holds the reference for you.

On modern PHP prefer spl_object_id(): an int is faster and smaller, and this function is essentially a hex rendering of it.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Marking already-visited nodes while recursively walking an object graph to avoid infinite loops.
  • Attaching computed metadata to objects in a map without modifying the objects themselves.
  • Deduplicating a list of objects where the same instance may appear more than once.

Frequently asked questions

What is the difference between spl_object_hash and spl_object_id?

spl_object_id() returns an int and spl_object_hash() returns that same id as a zero-padded hex string. Prefer spl_object_id() on PHP 7.2 and later; it is faster and uses less memory.

Is spl_object_hash unique forever?

No. It is unique only among objects alive at the same moment. After an object is destroyed its identifier can be reused by a new object.

Does spl_object_hash change when I modify the object?

No. It is based on the object's internal handle, not its properties, so it is stable for the object's whole lifetime and is not a content checksum.

More examples

Deduplicating a list of object references

Five entries collapse to two, because identity is decided by handle rather than by value. Note that $unique keeps real references, which is also what stops the hashes in $seen from being recycled mid-loop.

Example · php
<?php
$x = new stdClass();
$y = new stdClass();
$list = [$x, $y, $x, $y, $x];

$seen = $unique = [];
foreach ($list as $obj) {
    $h = spl_object_hash($obj);
    if (!isset($seen[$h])) { $seen[$h] = true; $unique[] = $obj; }
}
echo count($list), ' -> ', count($unique), "\n";

Contents do not affect the hash

Two objects with identical properties get different hashes, and mutating one does not change its hash. If you need a value fingerprint, hash a serialisation instead, as the last line shows.

Example · php
<?php
$p = new stdClass();
$p->name = 'same';
$q = new stdClass();
$q->name = 'same';

var_dump(spl_object_hash($p) === spl_object_hash($q)); // identical data
var_dump($p == $q, $p === $q);
$p->name = 'changed';
var_dump(spl_object_hash($p) === spl_object_hash($p));
echo md5(serialize($q)), "\n";

Discussion

  • Be the first to comment on this lesson.