【发布时间】:2016-02-01 14:32:57
【问题描述】:
我正在使用以下代码:
$input = new DateTime(filter_input(INPUT_GET, 'date'));
$input->modify('midnight');
echo $input->format(DateTime::RFC3339) . "\n";
$end = $input;
$end->modify('+3 hours');
echo $input->format(DateTime::RFC3339) . "\n";
echo $end->format(DateTime::RFC3339) . "\n";
输出如下:
2016-02-01T00:00:00-5:00
2016-02-01T03:00:00-5:00
2016-02-01T03:00:00-5:00
第二行的输出不应该和第一行一样吗?
据我了解,通过引用分配变量需要使用 $a = &$b,所以我使用的 ($a = $b) 应该是值。所以在$end 上调用的函数也不应该修改$input,对吗?我错过了什么?
【问题讨论】:
-
不要将标量变量与对象实例混淆.....实例始终是指针
-
来自PHP Docs -
When assigning an already created instance of a class to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. A copy of an already created object can be made by cloning it.