Question about var_dump output
Asked Answered
R

1

10

When I var_dump an object, the output looks like this:

object(XCTemplate)#2477 (4) {
  ["id"]=>
  string(1) "1"
  ["attributes"]=>
  array(0) {
  }
  ["db_table_name"]=>
  string(14) "template_names"
  ["cache"]=>
  array(0) {
  }
}

XCTemplate is its class, of course, but what does the integer (here: 2477) after the # mean?

Ramer answered 21/9, 2010 at 17:15 Comment(0)
T
8

It's a unique id associated with that particular instance of XCTemplate. AFAIK this is not documented, and also there is no way to get it (other than using var_dump()); and I've looked at the Reflection class.

From what I've seen:

  • The ids are unique to every instantiation; starting at 1 and incrementing by 1 with every new object. This includes every object; they don't have to be of the same class.
  • Destroying an instance (eg: through unset) frees up its id and the next instantiated object can (and will) use it.
  • It's not related to the variable; eg:

    $foo = new Foo();
    var_dump($foo);
    $foo = new Foo();
    var_dump($foo);
    

    Will produce different id's for different instantiations.

  • This is not the same as resource ids, where you can just convert to int to get the id:

    $resource= curl_init();      
    var_dump($resource);       // resource #1 of type curl
    print(intval($resource));  // 1
    print((int) $resource);    // 1
    
Terrigenous answered 21/9, 2010 at 17:22 Comment(5)
To add, on each instantiation, this number is incremented. If an instance is deleted (unset), that instance's # will then be reused on for the next instance.Egwin
Also, it's not an instance id of a particular class, but of any class.Egwin
Does this mean that 2477 objects have been initialized throughout the whole script? :ORamer
@Daniel Yeah, that's what it looks like. Are you using a heavy framework?Terrigenous
I do, it heavily makes use of MVC. Still got a really snappy performance. Also, thanks a lot for your very detailed answer!Ramer

© 2022 - 2024 — McMap. All rights reserved.