spl_object_id()

Available since PHP 5.2, the spl_object_hash() function exposes the PHP interpreter’s internal hashtable identifier for a given object in string format:

$o = new stdClass;

var_dump(spl_object_hash($o));

Executing the code shown above will print the output shown below:

string(32) "000000007952102b00000000195157c7"

This string can be used to uniquely identify an object as long as the object is not garbage-collected. Once the object is garbage-collected the string is no longer unique as the internal hashtable identifier it represents can be reused for new objects. This hash is not a direct representation of an object’s hashtable identifier as that is randomized before the string is generated.

A common use case for the string returned by spl_object_hash() is the usage as a key when storing objects in arrays. Using the SplObjectStorage class for this is usually the better approach, though.

The new spl_object_id() function provides direct access to an object’s identifier as a signed integer (and without the randomization applied by spl_object_hash()):

$o = new stdClass;

var_dump(spl_object_id($o));

Executing the code shown above will print the output shown below:

int(1)

As is the case for spl_object_hash() (and was discussed above), the identifier of an object is only unique during its lifetime. When the object is destroyed then its identifier will be reused.