如何更改 PHP 中的静态变量值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9497161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:57:33  来源:igfitidea点击:

How do I change a static variables value in PHP?

phpstatic-variables

提问by Weblurk

This is a simplified version of what I want to accomplish:

这是我想要完成的简化版本:

In my script I want a variable that changes true and false everytime the script is executed.

在我的脚本中,我想要一个每次执行脚本时都会更改 true 和 false 的变量。

<?php
    static $bool = true;

    // Print differente messages depending on $bool
    if( $bool == true )
        echo "It's true!";
    else
        echo "It's false!";

    // Change $bools value
    if( $bool == true )
        $bool = false
    else
        $bool = true;
?>

But obviously what I'm doing is wrong. The variable $boolis constantly trueand I haven't fully grasped the concept of static variables I presume. What am I doing wrong?

但显然我的做法是错误的。变量$bool是不断的true,我还没有完全掌握我假设的静态变量的概念。我究竟做错了什么?

回答by PatrikAkerstrand

PHP is not able to keep variable values between requests. This means that each time your script is called, the $bool-variable will be set to true. If you want to keep the value between requests you have to use sessionsor, if you want the variable shared between sessions, some caching mechanism like APCor Memcache.

PHP 无法在请求之间保留变量值。这意味着每次调用您的脚本时,$bool-variable 都将设置为 true。如果您想在必须使用的请求之间保留值,sessions或者,如果您希望在会话之间共享变量,则可以使用一些缓存机制,例如APCMemcache

Also, staticis used in PHP to declare a variable shared on the class level. It is thus used in classes, and accessed like self::$variableName;or Foo::$variableName

此外,static在 PHP 中用于声明在类级别共享的变量。因此它在类中使用,并像self::$variableName;Foo::$variableName

You can read more about static properties here. From the docs:

您可以在此处阅读有关静态属性的更多信息。从文档:

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

将类属性或方法声明为静态使它们无需实例化即可访问。声明为静态的属性不能用实例化的类对象访问(尽管静态方法可以)。

Also, note that the word statichas been overloaded since PHP 5.3, and can also be used to denote Late Static Binding, by use of static::

另外,请注意该词static自 PHP 5.3 起已被重载,也可用于表示Late Static Binding,通过使用static::

回答by addex03

A static value will not persist over executions. Every time the script is executed $bool is initialized. I think you should persist this value in a file to keep it simple.

静态值不会在执行过程中持续存在。每次执行脚本时 $bool 都会被初始化。我认为您应该将此值保留在文件中以保持简单。

回答by BenM

I think you need to better understand the point of a static variable. The storage for the variable is allocated (and deallocated) on the call stack, so from a software engineering point of view, its value cannot be changed in run time.

我认为您需要更好地理解静态变量的意义。变量的存储在调用堆栈上分配(和释放),因此从软件工程的角度来看,它的值不能在运行时更改。

There are better solutions as suggested above for this.

如上所述,有更好的解决方案。

回答by Bairu

It's an easy thing to use static keyword in php. Here I've used a static variable and a static method. Just try this out.

在 php 中使用 static 关键字很容易。这里我使用了一个静态变量和一个静态方法。试试这个。

<?php
   class Test{
        protected static $myVar;
        public static function printHello(){         
            self::$myVar = 'Hello'; //This will assign the value to the static variable $myVar
            echo self::$myVar;  //This prints the value of static variable $myVar
        }
    }
    Test::printHello();
?>