BasharIT
BasharIT

Reputation: 51

What are the numbers in var_dump result?

What do the numbers #11 and (0) mean in this var_dump result:

object(PDO)#11 (0) { }

I have a class and I made from it an object and used it in multiple places.

Upvotes: 3

Views: 280

Answers (1)

Dai
Dai

Reputation: 155145

I don't know either, so let's find out together, by looking at the source-code for var_dump! (look for PHP_FUNCTION(var_dump)).

(For TL;DR, jump to the end)

  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.


Regarding how ->handle values are determined and what they mean:

  • The Z_OBJ_HANDLE_P macro is Z_OBJ_HANDLE(*(zval_p))
    • The Z_OBJ_HANDLE macro is (Z_OBJ((zval)))->handle
    • The Z_OBJ macro is (zval).value.obj
    • So Z_OBJ_HANDLE_P(x) is the same as x.value.obj->handle
  • Note that instances of user-defined classes and PHP "resources" are both "objects" and both have a uint32 handle member (but implemented separately).
    • You can see this if you compare _zend_object with _zend_resource in zend_types.h
  • For "resources" (built-in objects provided by the PHP environment), PHP maintains a list of resources in an execution environment. When a new resource is created it's added to the list by zend_list_insert (which then uses the ZVAL_NEW_RES macro). The ->handle value is the index into that list (though I'm unsure if it starts at 0 or 1 or some other base).
  • For PHP class objects ("user types", etc), the zend_objects_store_put function is used which adds the object to the objects_store list and returns the the index of the item in the list (so it's conceptually similar to zend_list_insert).
    • Again, I don't know what the initial or base value is (e.g. 0, 1, or something else).

TL;DR:

So object(PDO)#11 (0) { } means:

  • the object is an instance of the PDO class.
  • the object is the 11th (probably) object created during the processing of the current HTTP request.
  • the object is its own top-level object rather than an object property reference.

Upvotes: 6

Related Questions