#是什么?对象的 var_dump 中的 object(someClass) 旁边?我有一个推论。我说得对吗?

发布于 2024-12-25 20:17:24 字数 1163 浏览 2 评论 0原文

这是代码&它的输出我用来得出下面的推论:

 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);  

输出:

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) {

}

var_dumpobject(someClass)行旁边的#对象的 code> 实际上是 #。在哪里,

count 是对象 / 对象的 zval 数量,无论到目前为止已创建的对象属于哪个类。对于创建的每个对象,它都会不断增加当 zval 的引用计数达到零(即垃圾回收)时,它会减 1。

我说得对吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦欢 2025-01-01 20:17:24

该数字是 Z_OBJ_HANDLE_PP(struc),其中 struc 是一个 zval,它通向 Z_OBJVAL(zval).handle,其中导致(zval).value.obj

另请参阅 http://php.net/manual/en/internals2.variables。 intro.php

简而言之,我想说它是对象标识符 以十进制书写形式(ref):

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

而不是计数曾经创建的对象的数量。

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.

清欢 2025-01-01 20:17:24

不,它是对对象实例的内部引用,如果您

var_dump($obj1); 

再次这样做,它仍然是 id #2

编辑

如果您的

var_dump(new stdClass);

PHP 正在创建 stdClass 的新实例并使用 var_dump 转储它,给你实例#7。但是,由于此实例是瞬态的(您没有将其分配给任何变量),因此它会立即再次被销毁,因此对象 id #7 再次可用于分配给您创建的下一个对象

$obj6 = new stdClass; 

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; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文