PHP强制赋值



我有

$today = new DateTime();
$tomorrow = $today->add(new DateInterval("P1D"));
$day = $tomorrow;
$day->add(new DateInterval("P1D"));

在此之后,$day将具有$tomorrow,的引用,这意味着,如果我像这样更改$day:

$day->add(new DateInterval("P1D"));

则$tomorrow也将改为后天:

print_r($tomorrow);

,我不想那样。

目前我找到的按值复制对象的唯一解决方案是:

$day = clone $tomorrow;

但是我不喜欢这个。欢迎提出任何建议。

创建两个独立的对象

$today = new DateTime();
$tomorrow = new DateTime();
$tomorrow->add(new DateInterval("P1D"));

最新更新