Get Instance ID of an Object in PHP

Posted by Alix Axel on Stack Overflow See other posts from Stack Overflow or by Alix Axel
Published on 2010-05-20T09:12:46Z Indexed on 2010/05/20 10:10 UTC
Read the original article Hit count: 270

Filed under:
|
|
|
|

I've learn a while ago on StackOverflow that we can get the "instance ID" of any resource, for instance:

var_dump(intval(curl_init()));  // int(2)
var_dump(intval(finfo_open())); // int(3)
var_dump(intval(curl_init()));  // int(4)
var_dump(intval(finfo_open())); // int(5)
var_dump(intval(curl_init()));  // int(6)

I need something similar but applied to classes:

var_dump(intval(new stdClass())); // int(1)
var_dump(intval(new stdClass())); // int(1)
var_dump(intval(new stdClass())); // int(1)
var_dump(intval(new stdClass())); // int(1)
var_dump(intval(new stdClass())); // int(1)

I'm using stdClass just has an example here, but as you can see, it's not the output I was hoping for.


I just did some more testing and I found that var_dump() can see the instance ID of an object:

var_dump($a = new stdClass()); // object(stdClass)#1 (0) { }
var_dump($b = new stdClass()); // object(stdClass)#2 (0) { }
var_dump($c = new stdClass()); // object(stdClass)#3 (0) { }

The same happens with resources of course:

var_dump(curl_init()); // resource(2) of type (curl)
var_dump(curl_init()); // resource(3) of type (curl)
var_dump(curl_init()); // resource(4) of type (curl)

Is there any way to achieve the same effect in PHP?

© Stack Overflow or respective owner

Related posts about php

Related posts about resources