Home > Article > Backend Development > Detailed explanation of the difference between static and self in php
I was asked about it during the interview, but I had no choice but to give an answer. I would like to summarize it here.
Use self:: or __CLASS__
A static reference to the current class, depending on the class in which the current method is defined:
Using static:: is no longer used Resolves to the class in which the current method is defined, but is calculated at actual runtime. It can also be called "static binding" because it can be used for (but is not limited to) calls to static methods.
Static binding is a function added in PHP 5.3.0 for referencing statically called classes in the inheritance scope
In simple terms,
self refers to which class it is written in, and it is this class that is actually called.
static represents the class used, which is the static you write in the parent class, and then it is overridden by the subclass. What is used is the method or attribute of the subclass
<?phpclass Person{ public static function name() { echo "111"; echo "<br />"; } public static function callself() { self::name(); } public static function callstatic() { static::name(); } }class Man extends Person{ public static function name() { echo "222"; echo "<br />"; } } Man::callself(); // output: 111Man::callstatic(); // output: 222?>
The above is the detailed content of Detailed explanation of the difference between static and self in php. For more information, please follow other related articles on the PHP Chinese website!