D
D
deadkrolik2012-02-08 18:49:58
PHP
deadkrolik, 2012-02-08 18:49:58

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

5 answer(s)
A
Anatoly, 2012-02-08
@taliban

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();

O
okneigres, 2012-02-08
@okneigres

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 .

S
Sergey Beresnev, 2012-02-09
@sectus

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 ..."?

S
Sergey Beresnev, 2012-02-10
@sectus

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.

S
shagguboy, 2012-02-08
@shagguboy

mine in the manual
www.php.net/manual/en/language.oop5.late-static-bindings.php
everything is described much more clearly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question