spl_object_id

Returns a unique integer identifier for an object, stable for as long as that object exists.

Signature
spl_object_id(object $object): int
  • Added in PHP 7.2.0

Parameters

ParameterTypeDescription
$objectobjectAny object.

Return value

intAn integer that is unique among all objects currently in memory and constant for the life of the object.

Errors

Throws a TypeError if the argument is not an object.

spl_object_id() hands back the object's internal handle as an int. It is the modern replacement for spl_object_hash(): same guarantees, but an integer key instead of a 32-character string, which means less memory and faster array lookups.

The number identifies the instance, never the data. Two objects with identical properties get different ids; an object whose properties all change keeps the same id.

When to reach for it

  • Cycle detection in recursive serialisers, diff tools and graph walkers.
  • Keying an array of per-object state where you also keep the objects themselves alive.
  • Debug output — printing the id next to the class name makes it obvious in a log whether two lines refer to one instance or two.

What trips people up

Ids are recycled. When an object is garbage collected, its number is put back in the pool and the next new can receive it. A map built from ids for objects you no longer hold a reference to will eventually associate stale data with an unrelated object — a bug that only shows up under memory pressure, which makes it very hard to reproduce.

The safe pattern is to store the object alongside its id, or to use SplObjectStorage, which keeps a reference and handles the bookkeeping. Also do not persist these ids: they are meaningless in another request or process.

Example

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

When to use it

  • Detecting cycles while recursively converting an object graph to an array.
  • Tagging log lines with an instance id so repeated calls can be traced to one object.
  • Keying per-object state in a hot loop where the string keys of spl_object_hash would cost too much.

Frequently asked questions

What is the difference between spl_object_id and spl_object_hash?

They identify the same thing, but spl_object_id() returns an int while spl_object_hash() returns it as a zero-padded hex string. Use spl_object_id() unless you specifically need a string key.

Is spl_object_id stable across requests?

No. It is an in-memory handle, valid only within the current process. Never store it in a database, a session or a cache.

Can two objects have the same spl_object_id?

Not at the same time. But once an object is destroyed its id can be reused by a newly created object, so ids are unique only among live objects.

More examples

Ids are reused after an object is freed

On a normal CLI run the new object claims the freed handle and the comparison is true. That is exactly how an id-keyed map goes wrong when it does not hold the objects it describes.

Example · php
<?php
$a = new stdClass();
$idA = spl_object_id($a);
unset($a);

$b = new stdClass();
$idB = spl_object_id($b);

echo "freed id: $idA, new id: $idB\n";
var_dump($idA === $idB);
echo "lesson: keep a reference if the id is a key\n";

Cycle-safe object graph walk

Two nodes are visited, then the walk detects that it has returned to the first id and stops instead of looping forever. Both objects stay referenced by $a for the whole loop, so no id can be recycled underneath it.

Example · php
<?php
$a = new stdClass();
$b = new stdClass();
$a->next = $b;
$b->next = $a;   // cycle

$seen = [];
for ($node = $a; $node !== null; $node = $node->next ?? null) {
    $id = spl_object_id($node);
    if (isset($seen[$id])) { echo "cycle at #$id\n"; break; }
    $seen[$id] = true;
    echo "visited #$id\n";
}

Discussion

  • Be the first to comment on this lesson.