Answer the question
In order to leave comments, you need to log in
The subtlety of Late Static Binding in PHP
Late Static Binding is a feature introduced in php 5.3. And it seems to be quite understandable and logical. But in the above link there is literally one sentence that baffles me:
Some people describe static:: as the $this-> resolution for static calls, this is not true as $this-> follows the rules of inheritance while static:: doesn't.
After all, according to the meaning, it seems that it turns out that static:: calls are the same as this, only for static members of the class, that is, which class will be chosen - a descendant or parent, is decided directly at execution. And smart people write that static:: "does not follow the rules of inheritance". What are these inheritance rules? How should this phrase be understood?
Answer the question
In order to leave comments, you need to log in
I suspect these:
Class A
{
static protected $_a = 'a';
static public function showA()
{
echo self::$_a;
}
}
class B extends A
{
static protected $_a = 'b';
}
B::showA();
Rather, it means that if you have inheritance A1-> A2 -> A3 and you have constructed an object A3, then there may be a situation that there is $this (A3) in the class method A1, but static:: will point to A2 .
Below is just an explanatory example. We call a method through the heir, which calls through the static method of the parent, which is not overridden in the heir. The method is called in the context of the parent, not the child. Those. echo __CLASS__ - shows the class in which the method is defined, and not the one through which it was called.
Isn't that the rabbit "Bunny-bunny, jump-jump ..."?
The above was wrong, that example does not explain anything. I wrote code in different ways, made assumptions ... but I didn’t succeed in making inheritance work somehow wrong.
But I have another guess: this dude is wrong.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question