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.
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"
}
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 thevar_dump
,#number
tells the order in which the object where instanciated.
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 seemsObject()#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, theObject()#number
is carried over if a different object is evaluated. TheObject()#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
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 . 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 ofvar_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);
TheZ_OBJ_HANDLE_P(struc)
macro expands roughly to(*struc).value.obj->handle
- it is looking up thehandle
member from the C struct representing the object. That is initialised in a function calledzend_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 thehandle
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.