Overview
I am learning design patterns. There was an article about the singleton pattern before. I re-read this article and found that my grasp of the static keyword is not very reliable, so I will review it again.
static keyword
The static keyword is introduced in the PHP manual as follows:
Copy code The code is as follows:
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).
The general meaning is that after declaring the properties and methods of a class as static, you can directly access the static properties and methods without instantiating the object.
The characteristics of static members and methods in PHP are as follows:
1. Static members cannot be accessed through instances of the class, but static methods can.
2. Static members cannot be accessed through the -> operator.
3. In the scope of a static method, the $this keyword cannot appear, which means that ordinary member variables cannot be accessed in a static method.
4. Static members and methods can be accessed directly through the class name without instantiating the object.
Late Static Bindings
The following content is excerpted from the PHP manual:
Copy code The code is as follows:
Since PHP 5.3.0, PHP has added a feature called late static binding for referencing statically called classes in an inheritance scope.
To be precise, the working principle of late static binding is to store the class name in the previous "non-forwarding call". When making a static method call, the class name is the one explicitly specified (usually on the left side of the :: operator); when making a non-static method call, it is the class to which the object belongs. The so-called "forwarding call" refers to static calls made in the following ways: self::, parent::, static:: and forward_static_call(). You can use the get_called_class() function to get the class name of the called method, and static:: points out its scope.
To understand this feature, you can refer to the examples in the manual
self vs static
Use a demo to directly explain the difference between self and static.
self example:
Copy code The code is as follows:
class Vehicle {
Protected static $name = 'This is a Vehicle';
Public static function what_vehicle() {
echo get_called_class()."n";
echo self::$name;
}
}
class Sedan extends Vehicle {
Protected static $name = 'This is a Sedan';
}
Sedan::what_vehicle();
Program output:
Copy code The code is as follows:
Sedan
This is a Vehicle
static example:
Copy code The code is as follows:
class Vehicle {
Protected static $name = 'This is a Vehicle';
Public static function what_vehicle() {
echo get_called_class()."n";
echo static::$name;
}
}
class Sedan extends Vehicle {
Protected static $name = 'This is a Sedan';
}
Sedan::what_vehicle();
Copy code The code is as follows:
Sedan
This is a Sedan
Looking at the last article, I haven’t updated my blog for more than a month. Being busy is part of it, but the main thing is that I have slacked off. I have to persevere in the future. This article was written with a bit of emotion.