Reputation: 87
var_dump(object) returns: object()#1
I really want to know what's the difference between object()#1, object()#2, and object()#3.
Any help is appreciated.
Upvotes: 4
Views: 3091
Reputation: 97718
From a user's point of view, the number is an arbitrary ID which can be used to see if two variables point to the same object; it is unique across all objects in a program, regardless of class and scope.
It can be retrieved directly using the spl_object_id
function. For instance:
class Test {}
$test1 = new Test;
$test1_id = spl_object_id( $test1 );
$test2 = new Test;
$test2_id = spl_object_id( $test2 );
var_dump( $test1 );
var_dump( $test2 );
var_dump( $test1_id );
var_dump( $test2_id );
Will output something like this:
object(Test)#1 (0) {
}
object(Test)#2 (0) {
}
int(1)
int(2)
The actual number should not be relied on as having any meaning, but in practice each object created is assigned a sequential ID, resetting at the start of each execution (HTTP request, or script invocation).
To give a very quick technical explanation, inside the definition of var_dump
, this is the line that produces the output:
php_printf("%sobject(%s)#%d (%d) {\n", COMMON, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0);
The Z_OBJ_HANDLE_P(struc)
macro expands roughly to (*struc).value.obj->handle
- it is looking up the handle
member from the C struct representing the object. That is initialised in a function called zend_objects_store_put
which has this line:
handle = EG(objects_store).top++;
EG
stands for "Execution Global", so this ultimately says "increment a counter in a per-execution store, and use that as the handle
for the new object".
Which is why my example above, run without any other code, will give IDs of 1 and 2: the first two items to be added to the store during that execution.
Upvotes: 0
Reputation: 146450
For objects with identical information (same class, same properties) it allows to determine whether they are the same instance. For example:
$a = new DateTime();
$b = $a; // Link to same instance
$c = clone $a; // Create a new copy
var_dump($a, $b, $c);
object(DateTime)#1 (3) {
["date"]=>
string(26) "2020-08-15 19:23:39.016441"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
object(DateTime)#1 (3) {
["date"]=>
string(26) "2020-08-15 19:23:39.016441"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
object(DateTime)#2 (3) {
["date"]=>
string(26) "2020-08-15 19:23:39.016441"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
Upvotes: 6
Reputation: 1045
Simply put:
first object to be declared
The behavior is slightly different if one object has been reinitialized (re-assigned) in the course of the script.
Example
class MyClass
{
}
$obj1 = new MyClass;
$obj2 = new stdClass;
$obj3 = new MyClass;
$obj3 = (object) [];
var_dump($obj3);
var_dump($obj1);
var_dump($obj4);
var_dump($obj2);
Result:
object(MyClass)#3 (0) {
}
object(MyClass)#1 (0) {
}
object(stdClass)#4 (0) {
}
object(stdClass)#2 (0) {
}
As you can see, no matter the order of the var_dump
, #number
tells the order in which the object where instanciated.
Upvotes: 2
Reputation: 1888
var_dump
dumps information about a variable. This function displays structured information about one or more expressions that include its type and value. Arrays and objects are explored recursively with values indented to show structure.
object()#1 ,object()#2,and object()#3 are the different object values inside a nested object which have been evaluated and given an id by var_dump
Read more here https://www.php.net/manual/en/function.var-dump.php
Edit 2
It seems Object()#number
is the order in which it finds some key/value pair inside a nested object. If an object doesn't contain any key/value pair and just contains another object (with key/value pair), that inner object is evaluated first and the #number is given accordingly. The outer object is evaluated last.
Also, the Object()#number
is carried over if a different object is evaluated. The Object()#number
starts from where it ended in the first object
Here is an example. This code will decode the JSON and dump the output of the object
<?php
$a= '{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}';
$b ='{
"entry1": {
"entry2": {
"title": 2,
"entry3": {
"title": 4,
"title" : 5
}
}
}
}';
$json1 = json_decode($a);
$json2 = json_decode($b);
var_dump($json1);
var_dump($json2);
Output
object(stdClass)#6 (1) {
["glossary"]=>
object(stdClass)#1 (2) {
["title"]=>
string(16) "example glossary"
["GlossDiv"]=>
object(stdClass)#2 (2) {
["title"]=>
string(1) "S"
["GlossList"]=>
object(stdClass)#5 (1) {
["GlossEntry"]=>
object(stdClass)#3 (7) {
["ID"]=>
string(4) "SGML"
["SortAs"]=>
string(4) "SGML"
["GlossTerm"]=>
string(36) "Standard Generalized Markup Language"
["Acronym"]=>
string(4) "SGML"
["Abbrev"]=>
string(13) "ISO 8879:1986"
["GlossDef"]=>
object(stdClass)#4 (2) {
["para"]=>
string(72) "A meta-markup language, used to create markup languages such as DocBook."
["GlossSeeAlso"]=>
array(2) {
[0]=>
string(3) "GML"
[1]=>
string(3) "XML"
}
}
["GlossSee"]=>
string(6) "markup"
}
}
}
}
}
object(stdClass)#10 (1) {
["entry1"]=>
object(stdClass)#9 (1) {
["entry2"]=>
object(stdClass)#7 (2) {
["title"]=>
int(2)
["entry3"]=>
object(stdClass)#8 (1) {
["title"]=>
int(5)
}
}
}
}
Correct me if I am wrong
Upvotes: 1