# `spl_object_id()`

> Section in “New and Extended Functionality › Language Features” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/language-features/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:

``` php
$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()`):

``` php
$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.

---

Previous: [Asynchronous Signal Handling](https://php7explained.com/new-and-extended-functionality/language-features/asynchronous-signal-handling/index.html.md) | Next: [get_mangled_object_vars()](https://php7explained.com/new-and-extended-functionality/language-features/get_mangled_object_vars/index.html.md)
