How to find Object ID in PHP?

PhpObject

Php Problem Overview


I'm using PHP 5.2. I'd like to find a way to output a unique id for every object, so it's easy when looking over logs to see which objects are the same.

In Ruby, I'd just say object.object_id to get Ruby's internal identifier for the object. There doesn't seem to be an obvious way to do this in PHP.

Is there is a built-in way of doing this? If there isn't, can you offer any other suggestions?

Php Solutions


Solution 1 - Php

Use spl_object_hash() for that.

It returns an unique identifier for each object instance, and not the name of the class, so it seems more suitable for you.

Edit:

For PHP < 5.2.x users, see this answer.

Solution 2 - Php

There is currently no way to do this in PHP, as of version 5.3.6.

spl_object_hash() does not do what you want - because it recycles the identifiers when objects get deleted, this will lead to errors in (for example) an object-relational mapper trying to keep track of objects in a session.

The description at the top of the documentation page ("This function returns a unique identifier for the object. This id can be used as a hash key for storing objects or for identifying an object.") is wrong - the truth is revealed in the note on that page: "When an object is destroyed, its hash may be reused for other objects", or in other words, the function does not always return a unique identifier, and can not always be used for storing or identifying objects.

The technique demonstrated in this comment may work in some cases, but it not reliable and will not work consistently either, since attempting to access an undefined property will invoke the __get() and __set() magic methods, the results of which are unpredictable.

In conclusion, the short answer to your question (unfortunately) is "no" - there is no such method in PHP, and there is no way to write a method like this that will work consistently for any object.

If you would like to see this feature added to PHP, please vote and/or comment here:

http://bugs.php.net/bug.php?id=52657

Solution 3 - Php

⚠️ PHP 7.2.0 introduces spl_object_id()!

$test = (object)[];
var_dump(spl_object_id($test)); # int(1)
Caveat emptor(?):

> When an object is destroyed, its id may be reused for other objects.

Solution 4 - Php

I know this is old topic, but i think i've found a solution.

The trick is in storing reference to each object in array with assigned key. You can then get object id by searching through that array and returning found key.

<?php

class objectMarker
{
    private $storage;

    function add($object) {
        $this->storage[] = $object;
    }

    function getId($object) {
        foreach ($this->storage as $id => $item) {
            if ($item === $object) {
                return $id;
            }
        }

        return null;
    }
}

$marker = new objectMarker;

$t1 = new stdClass;
$t2 = new stdClass;

$marker->add($t1);
$marker->add($t2);

echo $marker->getId($t1) . "\n";
echo $marker->getId($t2) . "\n";

unset($t1);

$t1 = new stdClass;
$marker->add($t1);

echo $marker->getId($t1) . "\n";

$t2->x = 1;
echo $marker->getId($t2) . "\n";

/* output:
0
1
2
1
*/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoelView Question on Stackoverflow
Solution 1 - PhpazkotokiView Answer on Stackoverflow
Solution 2 - Phpmindplay.dkView Answer on Stackoverflow
Solution 3 - Phpi336_View Answer on Stackoverflow
Solution 4 - PhprolacjaView Answer on Stackoverflow