Home > Article > Backend Development > What is static in PHP? Learn more about static properties and static methods
This article will take you to understand the static attributes and static methods in PHP, introduce the nature of static, and the difference between static variables and ordinary variables. I hope it will be helpful to everyone.
We mentioned before that calling member variables and methods requires the use of objects. But if We don’t want to instantiate, what if we directly access variables and methods? This uses the static function.
When defining variables and methods, add the static keyword in front of them to convert them into static, and you can pass Class name::double quotes for direct access.
We continue to rewrite the above example into static variables and static methods:
class Classname //定义一个类, 类名不区分大小写 { static public $name; //定义静态成员变量; static public $height=180; //初始化赋值 static public $weight, $nationality; //可以一个语句定义多个变量 static protected $age; static public function player($name,$height, $weight, $age, $sex) { //定义静态成员方法, 以及方法形参 self::$name=$name; //为成员变量赋值, 使用了代词self:: self::$weight=$weight; self::$height=$height; if (self::$height<185 && self::$weight<=85){ return self::$name.',符合要求'; }else{ return self::$name.',不太行'; } } } echo Classname::$height; //180, 通过类名::访问静态变量 echo Classname::player('xiaoming',180,80,22, 'Male'); //通过类名::访问静态方法;
As you can notice in the above example, I changed all $this are replaced by self. Because $this refers to the calling object, and self refers to the class itself where the method is located. Static methods can be called directly through the class name::. There is no object here, and \$this refers to If it is empty, the system will report an error. Therefore, when calling variables in a static method, you cannot pass $this.
Note that the variable following self:: must have the $ symbol.
Don’t underestimate this static. With it, it is not just that you can call it directly. The meaning of member variables and methods has fundamentally changed:
In Only static variables can be called in static methods, but ordinary variables cannot be called. Ordinary methods can call static variables. This is determined by the attributes of the static method, because ordinary member variables are bound to "objects". Static variables are bound to "classes".
I will explain the difference between static variables and ordinary variables in detail:
Ordinary The member variables are bound to the object. Different objects have their own set of member variables. The member variables of different objects have their own assignments. Although they may be the same, yours is yours.
Static variables are bound to the class. If the static variable changes, then the value will change in all objects of this class.
Static variables can also be used Access is through object::, but in fact, objects of the same class access the same static variable value. It can be understood that static variables are shared by the entire class, including its subclasses.
So even if one of the objects is destroyed, the static variable value will still be retained.
Subclasses can also override the static member variables of the parent class, but the parent class’s The static variables still exist, and these two static member variables are independent. They will be accessed separately according to the calling class name.
Let’s give an example:
class Shouji { static public $test; //定义一个静态变量 static function test5() //定义静态方法来操作并输出静态变量 { self::$test++; echo self::$test; } } class Shouji2 extends Shouji //定义一个子类 { static function test5() //定义子类的静态方法 { self::$test++; //访问并操作父类的静态变量 echo self::$test; } } $shouji1=new Shouji; //新建父类对象 $shouji1->test5(); //1, 通过对象调用静态方法 $shouji2=new Shouji; //新建另一个父类对象 $shouji2->test5(); //2, 在前一次操作基础上继续+1 $shouji3=new Shouji2; //新建子类对象 $shouji3->test5(); //3, 调用子类同名静态方法, 继续+1 echo Shouji::$test; //3, 通过父类::直接访问静态成员变量 echo $shouji1::$test; //3, 通过对象名::可以直接访问静态成员变量
Pass In the above example, we can also summarize the following points:
The subclass can override the static method of the parent class.
In the method To access static variables, you need to use the :: symbol. You cannot use $this;
Static methods and static variables will be inherited by subclasses.
Static variables It cannot be accessed by ->, but by :: (double colon)
The object can directly call static member methods through the object name ->, which is the same as calling ordinary methods It’s the same.
This article is reproduced from: https://juejin.cn/post/6977200691919978510
Author: The old man in the communication room
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is static in PHP? Learn more about static properties and static methods. For more information, please follow other related articles on the PHP Chinese website!