我遇到了 php 的存储问题。
我想要的是当每次我访问类 A 的函数 Main 时,我可以获得 变量 $temp 的值上次。
附言我知道我可以使用 session ,但它会浪费很多内存,而且不安全。
所以我想另寻他法。 下面是代码。
class A {
//initilize the value, how to make it just initialize once?
private static $temp = 0;
public function Main() {
echo "Last time I was=". $this->temp;
$this->temp += 1;
}
}
谢谢大家的帮助!等待你的想法
最佳答案
静态变量值存储在请求生命周期中。 session 足够安全,甚至可以在其中存储身份验证数据,因此如果您想在多个请求之间存储此数据,我建议使用 session 或数据库。
关于php - 在没有 session 的情况下存储上次运行时的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34533303/