【问题标题】:PHP Reference variable when working with recursion function使用递归函数时的 PHP 参考变量
【发布时间】:2020-07-08 20:57:37
【问题描述】:

我有 2 个递归函数来计算期初存量和期末存量。

public function getClosingStock($previousStock, $product, $periodType, $lower, $upper)
{
    $diffPeriod = 'diffIn' . Str::plural($periodType);
    $addPeriod = "add{$periodType}";

    $totalStocked = $this->getTotalStockInPeriod($product, $periodType, $lower);
    $totalIssued = $this->getTotalIssueInPeriod($product, $periodType, $lower);

    $currentStock = $previousStock + $totalStocked - $totalIssued;

    if ($upper->$diffPeriod($lower) > 0)
    {
        $currentStock = $this->getClosingStock(
            $currentStock, $product, $periodType, $lower->$addPeriod(), $upper
        );
    }

    return $currentStock;
}
public function getOpeningStock($previousStock, $product, $periodType, $lower, $upper)
{
    $subPeriod = "sub{$periodType}";

    return $this->getClosingStock($previousStock, $product, $periodType, $lower, $upper->$subPeriod());
}

基本上,这两个函数计算从有界下限时间到有界上限时间的股票。例如,下限为 2019 年 12 月 16 日,上限为 2020 年 3 月 31 日。因此他们将计算 3 月份的开盘和收盘股票。


$lower = Carbon::parse('2019-12-16');
$upper = Carbon::parse('2020-03-31');

echo $lower->toDateString() . '<br>'; // Print out: 2019-12-16
echo $upper->toDateString() . '<br>'; // Print out: 2020-03-31

$opening = $this->getOpeningStock(
    0, $product, 'month', $lower, $upper
);

echo $lower->toDateString() . '<br>'; // Print out: 2020-02-16
echo $upper->toDateString() . '<br>'; // Print out: 2020-02-29

$closing = $this->getClosingStock(
    0, $product, 'month', $lower, $upper
);

问题是在我运行 getOpeningStock 函数后,我的 2 变量 $lowerupper 被更改,它们没有保留我一开始设置的原始值。您可以在我上面的代码中看到注释中的差异。造成差异的原因是什么?

谢谢!

【问题讨论】:

  • 您可以将 $lower 和 $upper 都声明为常量变量,这样您的值就不会在递归函数中被更改/更新。

标签: php laravel recursion pass-by-reference


【解决方案1】:

克隆 $lower$upper,并在克隆的副本上调用 $addPeriod()$subPeriod(),然后递归传递:

public function getClosingStock($previousStock, $product, $periodType, $lower, $upper)
{
    $diffPeriod = 'diffIn' . Str::plural($periodType);
    $addPeriod = "add{$periodType}";

    $totalStocked = $this->getTotalStockInPeriod($product, $periodType, $lower);
    $totalIssued = $this->getTotalIssueInPeriod($product, $periodType, $lower);

    $currentStock = $previousStock + $totalStocked - $totalIssued;

    if ($upper->$diffPeriod($lower) > 0)
    {
    $cloned_lower = clone $lower;
    $currentStock = $this->getClosingStock(
        $currentStock, $product, $periodType, $cloned_lower->$addPeriod(), $upper
    );
    }

    return $currentStock;
}

public function getOpeningStock($previousStock, $product, $periodType, $lower, $upper)
{
    $subPeriod = "sub{$periodType}";

    $cloned_upper = clone $upper;
    return $this->getClosingStock($previousStock, $product, $periodType, $lower, $cloned_upper->$subPeriod());
}

【讨论】:

    猜你喜欢
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多