Detailed explanation of the use of static keyword in PHP

韦小宝
Release: 2023-03-20 20:02:01
Original
1582 people have browsed it

In a class in PHP, methods and attributes with the static keyword are called static methods and static attributes, so The methods and properties can be accessed directly through the class, without accessing through the corresponding instance of the class. This article describes how the static keyword is used in PHP. Students who have not yet understood the static keyword can read it together. Look!

In addition to the use of regular classes and methods, access control, there is also the static keyword static. Static variables can be local variables or global variables. Variables. When a program segment is executed, the static variable does not disappear. It still exists in the memory. It will still have the previous value when it is defined next time. It is often used to retain the previous value in recursion or sub-functions. It can be used Define variables and methods. I won’t go into detail about their functions. Let’s talk about how to use them. The following is a simple code:

class Human{
     public $name;
     protected $height;
 
     public static $sValue="Static Value in Human";
     public function eat($food){
         echo $this->name . "'s eating ". $food. "\n";
     }
}
 class Me extends Human
 {
     public $team="lsl";
     private $age="23";
 
     public static $president="zzy";
 
     public static function changePresident($newPrsdt){
         static::$president = $newPrsdt;
         echo parent::$sValue . "<br />";
     }
     //静态变量和方法操作
     private static $a="abc";
     public static function abc(){
         echo "<br />".self::$a;
         self::$a.="def";
    }
}
echo Me::$president. "\n";
Me::changePresident("web");
echo Me::$president. "<br />"; 
Me::abc();
Me::abc();
Copy after login


Define first Create a human Human, and then define a class Me to inherit Human. Then the members in Human can be used through Me objects, and the values ​​of member variables are valid throughout the use process. According to the code, you can see: static After the value of the variable is changed, the next call will be after the change. If it is an ordinary variable, it cannot be implemented. It can only be effective by changing the object, and it cannot be changed by methods inside the class. It is easier to pass the last two lines of code. It can be seen that the value of the static variable $a has changed after calling method abc twice. Static variables can be understood by thinking about them. They are also widely used. Here are some rules related to static keywords:

1. Generally, static properties are used to save the public data of a class.

2. Only static properties can be accessed inside a static method, including this class and the parent class.

3. Static members can be accessed without instantiating objects

4. To access static properties within this class, use self or static For keyword access, the variables followed must include $, such as: self::$a or static::$a

5. To access the static properties of the parent class, use parent, such as: parent ::$name

#6. When accessing static variables or methods outside the class, use the class name to access them directly without instantiation. Such as: Me::$pan and Me::abc()

There are so many specific points

I don’t understand static:

static Analysis of the difference between static variables and ordinary variables

The above is the detailed content of Detailed explanation of the use of static keyword in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!