Regarding the inheritance of static properties and methods on the Internet, the answers are all weird, so I just tried it directly with the code:
<code>class Base { public static $var = 'var'; public static function testStaticFun() { echo 'func'; } } class A extends Base { public function testSelf() { echo self::$var; } public function testParent() { echo parent::$var; } public function setSelf() { self::$var = 'self'; } public function setParent() { parent::$var = 'parent'; } public static function testStaticFun() { parent::testStaticFun(); echo 'over'; } } $objA = new A(); $objA->testSelf(); // var $objA->testParent(); // var $objA->setSelf(); $objA->testSelf(); // self $objA->testParent(); // self echo Base::$var; // self $objA->setParent(); $objA->testSelf(); // parent $objA->testParent(); // parent echo Base::$var; // parent Base::testStaticFun(); // func A::testStaticFun(); // func over </code>
So what exactly is static inheritance in php? Is there any difference in the behavior of static inheritance from other languages such as Java? Will the static properties and methods of the parent class be overridden? Is there only one copy of static properties after inheritance? What is the principle of inheritance implementation?
Regarding the inheritance of static properties and methods on the Internet, the answers are all weird, so I just tried it directly with the code:
<code>class Base { public static $var = 'var'; public static function testStaticFun() { echo 'func'; } } class A extends Base { public function testSelf() { echo self::$var; } public function testParent() { echo parent::$var; } public function setSelf() { self::$var = 'self'; } public function setParent() { parent::$var = 'parent'; } public static function testStaticFun() { parent::testStaticFun(); echo 'over'; } } $objA = new A(); $objA->testSelf(); // var $objA->testParent(); // var $objA->setSelf(); $objA->testSelf(); // self $objA->testParent(); // self echo Base::$var; // self $objA->setParent(); $objA->testSelf(); // parent $objA->testParent(); // parent echo Base::$var; // parent Base::testStaticFun(); // func A::testStaticFun(); // func over </code>
So what exactly is static inheritance in php? Is there any difference in the behavior of static inheritance from other languages such as Java? Will the static properties and methods of the parent class be overridden? Is there only one copy of static properties after inheritance? What is the principle of inheritance implementation?
Let’s break it down into several points:
1. Static members can be modified using access control keywords and can be inherited and overridden
In other words, subclasses can inherit the static variables and methods of the parent class... Follow "Keyword" Rules
2. If the subclass does not override, then the subclass actually calls the static method of the parent class
This is also very simple and clear. There is nothing to explain about the basic rules of inheritance
3. The static member holder is a class, not an object, so multiple instances of a class share the same static attribute. Modifying the static attribute in one instance will affect the static attribute in another instance
This is the key point. Understand it carefully. Single column mode also uses this feature.
So self::$var and parent::$var in the code actually point to $var in the parent class!
Finally, here is an example:
In singleton mode, $var can be understood as a database connection. After the parent class generates a connection, the inherited subclass can directly use this connection to connect to the database.
But if the subclass generates a connection $var by itself, then you need to distinguish whether it is self::$var or parent::$var when using it, because the two values may be different, and the connected databases are also different. What if it is the master and the slave. Ha ha.
happy coding~
I have answered this type of question several times here, such as this one. You will understand after reading it.
Yes, there is only one copy of static attributes!
So be careful when using static attributes to write singleton patterns.