search
HomeBackend DevelopmentPHP ProblemWhat is the difference between php const and static variables

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor