php中的静态变量不保存值

发布于 2025-01-14 06:42:32 字数 1921 浏览 4 评论 0原文

我编写了一个演示程序来描述我在 php 中使用静态变量时遇到的问题。我有一个单例类 History.php,它包含一个静态 $idx 变量,最初设置为 -1。 History 类中的 incIdx() 函数递增 $idx。该演示首先调用index.php,它获取History 的实例,重复调用incIdx() 并显示$idx 最终值。然后,单击按钮时,将调用 test.php,获取 History 实例,重复调用 incIdx(),并显示最终值。我不明白的是,index.php 创建的值不是 test.php 的起始值。也就是说,index.php 和 test.php 都以初始值 = -1 开始。 代码如下:

<?php
class History {
    private static int $idx;
    private static $instance = null;
    
    private function __construct() {
        $this::$idx = -1;
    }
    
    public static function getHistory() {
        if (self::$instance === null) {
            self::$instance = new History();
        } 
        return self::$instance;
    }
    
    public function incIdx() {
        $this::$idx++;
    }
    
    public function getIdx(): int {
        return $this::$idx;
    }
}

<?php
require_once "History.php";

$instance = History::getHistory();
for ($i = 0; $i < 5; $i++) {
    $instance->incIdx();
}
$value = $instance->getIdx();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Test Static</title>
    </head>
    <body>
        <h1>In index.php</h1>
        <h2>Current idx = "<?php echo $value; ?>"</h2>
        <button type="button"><a href="test.php">Click to continue</a></button>
    </body>
</html>

<?php
require_once "History.php";

$instance = History::getHistory();
for ($i = 0; $i < 5; $i++) {
    $instance->incIdx();
}
$value = $instance->getIdx();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Test Static</title>
    </head>
    <body>
        <h1>In test.php</h1>
        <h2>Current idx = "<?php echo $value; ?>"</h2>
    </body>
</html>```

I have written a demo program to describe a problem I'm having with static variables in php. I have a singleton class, History.php, that contains a static $idx variable intially set to -1. An incIdx() function in the History class increments $idx. The demo starts by calling index.php which gets an instance of History, repeatedly calls incIdx() and displays the $idx final value. Then, on clicking a button, test.php is called which gets the History instance, repeatedly calls incIdx(), and displays the final value. What I don't understand is, the value created by index.php is not the starting value for the test.php. That is, index.php and test.php both start with the initial value = -1.
The code follows:

<?php
class History {
    private static int $idx;
    private static $instance = null;
    
    private function __construct() {
        $this::$idx = -1;
    }
    
    public static function getHistory() {
        if (self::$instance === null) {
            self::$instance = new History();
        } 
        return self::$instance;
    }
    
    public function incIdx() {
        $this::$idx++;
    }
    
    public function getIdx(): int {
        return $this::$idx;
    }
}

<?php
require_once "History.php";

$instance = History::getHistory();
for ($i = 0; $i < 5; $i++) {
    $instance->incIdx();
}
$value = $instance->getIdx();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Test Static</title>
    </head>
    <body>
        <h1>In index.php</h1>
        <h2>Current idx = "<?php echo $value; ?>"</h2>
        <button type="button"><a href="test.php">Click to continue</a></button>
    </body>
</html>

<?php
require_once "History.php";

$instance = History::getHistory();
for ($i = 0; $i < 5; $i++) {
    $instance->incIdx();
}
$value = $instance->getIdx();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Test Static</title>
    </head>
    <body>
        <h1>In test.php</h1>
        <h2>Current idx = "<?php echo $value; ?>"</h2>
    </body>
</html>```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文