The static reference to the current class using self:: or __CLASS__ depends on the class defining the current method:
Using static:: is no longer parsed to define the class where the current method is located, but is calculated at the actual runtime. It can also be called "static binding" because it can be used (but is not limited to) calls to static methods.
Static binding is a feature added in PHP 5.3.0. It is used to refer to static calls within the inheritance scope.
The difference between static and self in the php class. You can often see self and static in the definition of the php class. When running, you often find that there is no difference in the results, but it is definitely not without differences, because there is no difference, why are there two?
1. The difference between the two
Static uses delayed binding
So it will cause static to know exactly whether it is a parent class or a child class call
For example, this shows that static is a smart child and he can accurately understand his relatives.
Self is a little stupid child, he only knows your own parents
2. See the code differently
It can be seen from the following code
Self points to the class that defines him, that is, he only knows his own parents
Static points to calling his class, that is, he can recognize his relatives
<?php class parents { protected static $name = 'I am your parents'; public static function who_self() { return self::$name; } public static function who_static() { return static::$name; } public static function get_self() { return new self(); } public static function get_static() { return new static(); } } class kinsfolk extends parents { protected static $name = 'I am your relative'; } var_dump(kinsfolk::who_self()); //I am your parentsvar_dump(kinsfolk::who_static()); //I am your relativevar_dump(kinsfolk::get_self()); //object(parents)#1 (0) {} var_dump(kinsfolk::get_static()); //object(kinsfolk)#1 (0) {}
To put it simply,
Self is written in which class, and this class is actually called.
static represents the class you use, which is the static you wrote in the parent class, and is overwritten by the subclass, using the subclass methods or properties.
Summarize
This is all about this article about the difference between static and self in the php class. For more related content on the difference between static and self in the php class, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!