Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the meaning of the result of var_dump (object)?

Tags:

php

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.

like image 447
Toffee Conmigo Avatar asked Oct 22 '25 22:10

Toffee Conmigo


2 Answers

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"
}
like image 144
Álvaro González Avatar answered Oct 25 '25 13:10

Álvaro González


Simply put:

  • #1 means first object to be declared
  • #2: second object to be declared
  • #3: etc.

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.

like image 41
Prince Dorcis Avatar answered Oct 25 '25 13:10

Prince Dorcis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!