关于 var_dump 输出的问题

10

当我使用 var_dump 函数打印一个对象时,输出的结果看起来像这样:

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

XCTemplate是它的类名,但是#后面的整数(这里是2477)代表什么意思呢?

1个回答

8

这是与特定的XCTemplate实例关联的唯一标识符。据我所知,这并没有记录在文档中,也没有其他方法可以获取它(除了使用var_dump());我已经查看了Reflection类。

从我所见:

  • 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
    

1
每次实例化时,都会增加这个数字。如果一个实例被删除(取消设置),那么该实例的编号将被重用于下一个实例。 - webbiedave
1
此外,它不是特定类的实例ID,而是任何类的实例ID。 - webbiedave
1
这是否意味着在整个脚本中已经初始化了 2477 个对象? :O - Daniel
2
@Daniel 是的,看起来是这样。你正在使用一个沉重的框架吗? - NullUserException
1
我做的项目非常依赖MVC,但仍然具有非常快速的性能。另外,非常感谢您详细的回答! - Daniel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接