Home  >  Article  >  Backend Development  >  What is the difference between php const and static variables

What is the difference between php const and static variables

青灯夜游
青灯夜游Original
2021-03-31 18:15:562535browse

Difference: Once defined, const cannot be changed, while variables modified by static can be changed. const can only modify class attributes, not class methods; static can modify attributes as well as methods.

What is the difference between php const and static variables

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

static and The difference between const variables

static variables

1.static static variables we can modify them , but we cannot modify const variables
2.static static variables can modify permissions
3.Similar to java, inside the class, satic The body of the modified method cannot access non-staic member variables of the class, but can only access the class's staic variables and the class's const constants

class staticTest1 {
    var $var1 = "hello";
    public static $var2 = "hellostatic"; //public, protected, private
    const var3 = "helloconst";
    public static function displayDifferent(){
###        echo $this->$var1."<br>";//不能访问普通变量
        echo staticTest1::$var2."<br>";//可以访问类的静态变量
        echo self::var3."<br>";//不能用$this::var3, self::var3代表当前类,$this::var3代表当前对象
    }
}

//You can call the method in two ways

//第一种,通过对象调用
$test1 = new staticTest1();
echo $test1->displayDifferent();
echo "<br>";
//第二种,通过类调用
echo staticTest1::displayDifferent();
echo "<br>";

By the way, "::" can only access static variables and methods for objects, and self can only use "::" to call members of the current class

const variables

1. Const variables can only modify member variables, not methods
2. No modification permission is required
3 .Because const variables belong to the entire class and do not belong to an object, they cannot be accessed through objects. For example, $this->constvariable does not allow

class constTest1 {
    var $var1 = "welcome";
//    public const pi = 3.14;//不能加修饰权限
    const pi = 3.14;
//    const function displayDifferent() {//function前不能加const
//        
//    }
   function displayDifferent() {
        echo self::pi."<br>";
//        echo $this::pi."<br>"; 
    }
    static function displayDifferent2() {
        echo self::pi."<br>";
//        echo $this::pi."<br>"; //这句话不行。
    }
}

two method calls

//第一种,通过对象调用
$test2 = new constTest1();
echo $test2->displayDifferent();
//第二种,通过类调用
//echo constTest1::displayDifferent();//对象名用"::"只能访问静态变量和方法,所以这个不行

echo constTest1::displayDifferent2();

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the difference between php const and static variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn