var_dump 结果中的数字是什么意思?

3
在这个var_dump结果中,数字#11和(0)代表什么意思:

对象(PDO)#11 (0) { }

我有一个类,并从它创建了一个对象并在多个地方使用。

2
#n 通常指的是资源句柄(套接字或文件指针)。 - mario
我有一个类,我从中创建了一个对象,并在多个地方使用它。- 我想你的意思是你已经从class创建了一个objectclass就像是对象的蓝图,而object是类的一个实例,即运行程序中的数据集合(想想USS Enterprise NCC-1701-D是一艘“Galaxy class”星舰:类是“Galaxy”,对象名称是“Enterprise”)。 - Dai
@mario 的确,#n 是一个对象或资源的“句柄”。在 OP 的情况下,PDO 是一个实际的 PHP class 中的 PHP object,而不是直接作为“resource”(当然,PDO 确实包装了资源)。 - Dai
1个回答

6

我也不知道,所以让我们一起来查找吧,查看 var_dump 的源代码!(查找 PHP_FUNCTION(var_dump))。

(如果太长,可直接跳到结尾)

  1. The PHP function var_dump is a wrapper for the C function php_var_dump.

  2. php_var_dump has a switch() statement to generate different output for each of PHP's basic types (numbers, strings, booleans, objects, etc), and we're interested in the object type.

  3. Within the case IS_OBJECT: case we see this:

    php_printf("%sobject(%s)#%d (%d) {\n", COMMON, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0);
    
  4. The #10 in your output comes from the #%d part of the format-string, which is the 3rd C variadic arg, and the (0) is the 4th C variadic arg.

    • the 3rd is Z_OBJ_HANDLE_P(struc)
    • the 4th is myht ? zend_array_count(myht) : 0
  5. Z_OBJ_HANDLE_P basically returns a unique identifier for an object in PHP (so your PDO instance is the 11th object (I think, see below) created in the processing of this request).

  6. The myht thing is more complicated: but if it's set it means you asked PHP to var_dump an object member property (rather than an object itself), e.g. var_dump( $foo->bar ) instead of var_dump( $foo ). If you aren't referring to an object-property then it just prints 0.

关于如何确定和理解->handle值的问题:
  • Z_OBJ_HANDLE_P宏是 Z_OBJ_HANDLE(*(zval_p))
    • Z_OBJ_HANDLE宏是(Z_OBJ((zval)))->handle
    • Z_OBJ宏是(zval).value.obj
    • 因此,Z_OBJ_HANDLE_P(x)x.value.obj->handle相同
  • 请注意,用户定义的类实例和PHP“资源”都是“对象”,并且都有一个uint32 handle成员(但是分别实现)。
    • 如果您将zend_types.h中的_zend_object_zend_resource进行比较,就可以看到这一点
  • 对于PHP环境提供的“资源”(内置对象),PHP在执行环境中维护资源列表。创建新资源时,zend_list_insert将其添加到列表中(然后使用ZVAL_NEW_RES宏)。 ->handle值是该列表的索引(尽管我不确定它是从0还是1或其他基础开始的)。
  • 对于PHP类对象(“用户类型”等),使用zend_objects_store_put函数将对象添加到objects_store列表中,并返回列表中项目的索引(因此在概念上类似于zend_list_insert)。
    • 同样,我不知道初始或基本值是什么(例如01还是其他值)。

简而言之:

object(PDO)#11 (0) { }表示:

  • 该对象是PDO类的一个实例。
  • 该对象是当前HTTP请求处理期间创建的第11个(可能)对象。
  • 该对象是自己的顶级对象,而不是对象属性引用。

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