我有两个 DateTimeImmtable
对象,并期望它们是相同的,但我很惊讶地发现它们不是。即,为什么下面的 false
?
<?php
$d = new \DateTimeImmutable('2018-01-01');
$e = new \DateTimeImmutable('2018-01-01');
var_dump($d === $e);
当然 $d == $e
的计算结果为 true
最佳答案
这与 DateTimeImmutable
对象无关,这只是 PHP 处理对象比较的方式。来自 the manual :
When using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class.
无论任何属性的值如何,使用此运算符比较任何两个不同的实例将始终返回 false。
关于php - 为什么 php 中的相同运算符 (===) 因 DateTimeImmutable 对象而失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49150909/