Why does an identical statement in php (===) fail with DateTimeImmutable objects?

I have two objects DateTimeImmtable, and expect them to be the same. I am surprised to see that this is not so. Those. why the following false?

<?php
$d = new \DateTimeImmutable('2018-01-01');
$e = new \DateTimeImmutable('2018-01-01');

var_dump($d === $e);

Of course $d == $eis rated astrue

+4
source share
2 answers

This is not related to objects DateTimeImmutable, this is just how PHP deals with object mapping. From the manual :

When using the identification operator (===), object variables are identical if and only if they refer to the same instance of the same class.

false, .

+2
$d = new \DateTimeImmutable('2018-01-01');
$e = new \DateTimeImmutable('2018-01-01');

var_dump($d);
var_dump($e);

object(DateTimeImmutable)[1]
  public 'date' => string '2018-01-01 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Paris' (length=12)
object(DateTimeImmutable)[2]
  public 'date' => string '2018-01-01 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Paris' (length=12)

PHP: , , 2

=== ( ), , - false

+2

Source: https://habr.com/ru/post/1694590/


All Articles