What is #<some-number> next to object(someClass) in var_dump of an object? I have an inference. Am I right?

This is the code & its output I used to draw the inference below:

 class a {
    public $var1;
    public $var2;
    }

 $obj0 = new a;
 var_dump($obj0);

 class b {
    public $var1;
    public $var2;
    public $var3;
    }

 $obj1 = new b;
 var_dump($obj1);

 $obj2 = new stdClass;
 var_dump($obj2);

 $obj3 = new stdClass;
 var_dump($obj3); 

 $obj4 = new stdClass;
 var_dump($obj4);

 $obj5 = new stdClass;
 var_dump($obj5);

 var_dump(new stdClass); 

 $obj6 = new stdClass;
 var_dump($obj6);  

The output:

object(a)#1 (2) {
  ["var1"]=> NULL
  ["var2"]=> NULL
}
object(b)#2 (3) {
  ["var1"]=> NULL
  ["var2"]=> NULL
  ["var3"]=> NULL
}
object(stdClass)#3 (0) {

}
object(stdClass)#4 (0) {

}
object(stdClass)#5 (0) {

}
object(stdClass)#6 (0) {

}
object(stdClass)#7 (0) {

}
object(stdClass)#7 (0) {

}

The #<some-number> next to the line object(someClass) in var_dump of an object is actually #<count>. Where,

count is the number of objects / zval's for objects irrespective of which class it belongs to that has been created till now. Which keeps getting incrementing for every object created & gets decremented by 1 when a refcount of a zval reaches zero i.e. Garbage Collection.

Am I right?


Solution 1:

That number is Z_OBJ_HANDLE_PP(struc) where struc is a zval which leads to Z_OBJVAL(zval).handle which leads to (zval).value.obj.

See as well http://php.net/manual/en/internals2.variables.intro.php

In short I would say it's the object identifier written in decimal form (ref):

php_printf("%sobject(%s)#%d (%d) {\n", COMMON, class_name, Z_OBJ_HANDLE_PP(struc), myht ? zend_hash_num_elements(myht) : 0);

And not the count of objects ever created.

Solution 2:

No, it's an internal reference to the object instance, if you did

var_dump($obj1); 

again, it would still be id #2

EDIT

In the case of your

var_dump(new stdClass);

PHP is creating a new instance of stdClass and dumping it using var_dump, giving you instance #7. However, because this instance is transient (you're not assigning it to any variable) it's being destroyed again immediately afterwards, so object id #7 is available again for allocation to the next object that you create with

$obj6 = new stdClass;