早上好,
我似乎对自己的代码视而不见,所以如果几周前编写的一段代码,我想征求一些额外的意见和建议。
守则(缩短)
class TrackingCostCollection {
private $available=0;
private $ordered=0;
private $differenceNumKits=0;
public function fill ($row) {
...
}
/**
* @var TrackingCostCollection $collection
*/
public function merge ($collection) {
$this->available += $collection->available;
$this->ordered += $collection->ordered;
$this->differenceNumKits= $this->available-$this->ordered;
}
}
问题
当通过其中一个数据字段对数据进行迭代和分组时,我遇到了存储在两个不同数组中的两个不同类实例包含相同数据的问题,尽管我不明白为什么?一旦我使用额外的“克隆”,问题就会消失,但我想了解为什么我的代码会以这种方式运行:
foreach ($arrValues as $row) {
...
$this->calculateStatistics($row);
}
private function calculateStatistics ($row) {
$fieldValue= $row['country'];
$collection = new TrackingCostCollection();
$collection->fill($row);
if (!isset($this->arrStatistics[$fieldValue])) {
$this->arrStatistics[$fieldValue] = $collection2;
} else {
/* @var TrackingCostCollection $previousCollection2 */
$previousCollection = $this->arrStatistics[$fieldValue];
$previousCollection->merge($collection2);
$this->arrStatistics[$fieldValue] = $previousCollection;
unset($previousCollection);
}
// using $collection instead of $collection2 will cause
// two array entries containing the same data
$collection2= clone $collection;
if (!isset($this->arrTotals[$fieldValue])) {
$this->arrTotals[$fieldValue] = $collection2;
} else {
/* @var TrackingCostCollection $previousCollection2 */
$previousCollection = $this->arrTotals[$fieldValue];
$previousCollection->merge($collection2);
$this->arrTotals[$fieldValue] = $previousCollection;
unset($previousCollection);
}
}
如果需要更多代码来诊断问题的原因,请告诉我,我会添加更多。
非常感谢您抽出宝贵时间!