php中的静态变量不保存值
我编写了一个演示程序来描述我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论