How to find object id in PHP?

I am using PHP 5.2. I would like to find a way to display a unique identifier for each object, so when viewing logs it is easy to see which objects are the same.

In Ruby, I just say object.object_id to get the Ruby internal identifier for the object. There seems to be no obvious way to do this in PHP.

Is there a built-in way to do this? If not, can you offer other suggestions?

+46
object php
Nov 21 '08 at 7:37
source share
4 answers

Use spl_object_hash() for this.

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

Edit:

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

+82
Nov 21 '08 at 7:46
source share

There is currently no way to do this in PHP starting with version 5.3.6.

spl_object_hash () does not do what you want - since it processes identifiers when objects are deleted, this will lead to errors (for example) in an object-relational mapmaker trying to track objects in a session.

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

The method demonstrated in this comment may work in some cases, but it is not reliable and will not work sequentially, since an attempt to access the undefined property will call the magic methods __get () and __set (), 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 such a method that will work sequentially for any object.

If you want this feature to be added to PHP, vote and / or comment here:

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

+15
Jun 13 '11 at 12:50
source share

I know this is an old topic, but I think I found a solution.

The trick is to store a reference to each object in an array with an assigned key. Then you can get the identifier of the object by searching this array and returning the key found.

 <?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 */ 
0
Jan 04 '16 at 21:06
source share

⚠️ PHP 7.2.0 now has spl_object_id () !

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

When an object is destroyed, its identifier can be reused for other objects.

0
May 10 '19 at 4:44
source share



All Articles