在重复调用方法 PHP 时递增计数器变量


increment a counter variable upon repeated call of a method, PHP

<html>
//index.php
<form action="" method="post"> 
  <label for="item">Item: </label>
    <select name="item">
        <option value="fruit tea">Fruit Tea</option>
         <option value="strawberries">Strawberries</option>
         <option value="coffee">Coffee</option>
    </select><br />
    <label for="qty">Qty: </label>
        <input type="text" name="qty" />
        <input type="submit" name="submit" value="submit" />
</form>

<?php
    @$qty = $_POST['qty'];
    @$item = $_POST['item'];
    $co = new Checkout( $pricing_rules );
    print $co->scan( $item ). ' has been scanned '.$co->increment(). ' times';
?>
</html>


<?php
/**
* Description of Checkout
*
* @author cookie
*/
class Checkout {
private $pricing_rules = array();
private $pcode;
public $item;

public function __construct( array $pricing_rules ) {
    $this->pricing_rules = $pricing_rules;
}
public function scan( $item ) {
    $this->increment();
    return $item;
}
private function prod_code( $pcode ) {
    $this->pcode = $pcode;
}

public function increment() {
    static $count = 0;
    $count++;
    return $count;
}
public function total() {
}

}
?>

它应该做什么...

如果我单击提交按钮四次,则说从下拉列表中选择草莓。脚本应打印到屏幕上:

草莓已被扫描 1 次

草莓已被扫描2次

草莓已被扫描3次

草莓已被扫描4次

等。。。。

随着$count跟踪增量。

它实际做什么:

它打印:

草莓已被扫描 2 次,无论我按多少次提交。并停在那里。我在这里错过了一些东西。

我看到它在做什么 - 在页面加载时,通过这一行对 increment(( 进行两次调用:

print $co->scan( $item ). ' has been scanned '.$co->increment(). ' times';

我试过了:

  if($_SERVER['REQUEST_METHOD'] == 'POST') $co->increment();

此外,这会$count递增 1,但会停在那里。我对此有点挣扎..

帮助

static变量对于当前脚本执行只是静态的。每次重新加载页面(提交时(时,变量都会丢失。为此,您需要将其存储在 $_SESSION . 事实上,有一个与您在$_SESSION基本使用手册中尝试的示例非常相似的示例。

// Call session_start() at the beginning of your script...
session_start();
// And modify your method to store count in the $_SESSION.
public function increment() {
    // initialize it if not already initialized
    $_SESSION['count'] = !isset($_SESSION['count']) ? 0 : $_SESSION['count'];
    $_SESSION['count']++;
    return $_SESSION['count'];
}

从 PHP 7.4 及更高版本开始,如果尚未声明 count 元素,请使用"null 合并赋值运算符"在 $_SESSION 数组中引入该元素,然后在从该方法返回该元素的同时++递增其值(修改其存储值(。

public function increment() {
    $_SESSION['count'] ??= 0;
    return ++$_SESSION['count'];
}

每次提交表单时,它都会向服务器发出新的HTTP请求,并且脚本会重新运行。 变量不会在一次运行到下一次运行时保留其值。

如果要在页面重新加载时保存变量,则需要按照Michael Berkowski的建议使用会话变量。但是,这只是在浏览器会话中。如果您需要更长的保留时间,您可以使用 Cookie,尽管用户可以删除这些 Cookie。 最后,如果你想要真正的永久内存,你可以使用数据库。

如果需要合并来自多个用户的数据,该数据库也适用。 例如,如果用户 A 扫描一个项目,然后用户 B 扫描该项目,并且您希望显示合并的总计。

除了Michael Berkowski的回答,我还想指出其他一些事情。如果函数应该是对象字段,为什么要在函数中声明$counter。既然它应该是一个对象字段,那为什么要static呢?如果它是静态的,它将计入给定点的所有Checkout实例。
此外,您在内部调用一次increment() scan(),然后再次调用它

print $co->scan( $item ). ' has been scanned '.$co->increment(). ' times';
//         ^ here $counter == 1                       ^ and here $counter == 2


这样的事情对我来说更有意义

class Checkout{
    private $counter;

    public __construct(){
        $this->counter = 0;
        // ......
    }
    // ..... 
    public function scan(){
        // ....
        $this->counter++;
    }
    public function getCounter(){
        $return $this->counter;
    }
}