This is the 7th day of my participation in Gwen Challenge

What is static?

We talked about calling member variables, calling methods, using objects. But what if we don’t want to instantiate and access variables and methods directly? This uses the static static function.

Class name :: = static; class name :: = static; class name :: = static;

Let’s go ahead and rewrite the above example as static variables and static methods:

{static public $name; static public $name; // Define static member variables; static public $height=180; Static public $value, $value; 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. ', meets the requirements; }else{return self::$name. } } } echo Classname::$height; Echo Classname::player('xiaoming',180,80,22, 'Male'); // Access static methods through the class name ::;Copy the code

Notice from the above example that I replaced all $this with self. Because $this refers to the calling object, self refers to the class of the method itself. Static methods can be called directly by the class name ::, where there is no object, \$this refers to empty, the system will report an error. So a variable called from a static method cannot be called through $this.

Note that self:: follows the variable with the $sign.

Static nature

Member variables and methods change the meaning of a member variable or method:

Only static variables can be called in a static method, not normal variables, which can be called in a normal method. This is determined by the properties of static methods, because ordinary member variables are bound to “objects”, whereas static variables are bound to “classes”.

Let me elaborate on the difference between static and normal variables:

  1. Common member variables are bound to objects, and different objects have their own set of member variables. Member variables of different objects have their own assignments, and while they may be the same, what’s yours is yours.

  2. Static variables are bound to classes. If the static variable changes, the value changes in all the objects of the class.

  3. Static variables can also be accessed via ::, but objects of the same class access the same static variable value. A static variable is shared by the entire class, including its subclasses.

  4. So even if one of the objects is destroyed, the static variable value remains.

  5. Subclasses can also override static member variables of the parent class, but the static variables of the parent class still exist, and the two static member variables are independent. Each is accessed based on the class name called.

Let’s take an example:

class Shouji { static public $test; Static function test5() {self::$test++; echo self::$test; }} class Shouji2 extends Shouji {self::$test++; Echo self::$test; echo self::$test; } } $shouji1=new Shouji; $shouji1->test5(); $shouji2=new Shouji; $shouji2->test5(); $shouji3=new Shouji2; $shouji3=new Shouji2; $shouji3->test5(); Echo Shouji::$test; echo Shouji::$test; Echo $shouji1::$test; echo $shouji1::$test; //3, static member variables can be accessed directly by the object name ::Copy the code

From the above example, we can also draw the following conclusions:

  1. Subclasses can override static methods of their parent class.

  2. To access static variables within a method, use the :: symbol. Can’t use $this;

  3. Both static methods and static variables are inherited by subclasses.

  4. Static variables cannot be accessed by ->, but by :: (double colon)

  5. Static member methods can be called directly by the object name ->, just like normal methods.

Thank you for reading, if there are inaccurate and wrong places, welcome to leave a message to correct, I will be timely correction, thanks!

Welcome to exchange with me, learn from each other and grow up together

Summary is not easy, do not reprint without permission, otherwise don’t blame old uncle you are welcome!

References:

WWW.PHP.NET PHP official documentation