0

Ok, the title is hard to understand, but I was trying to understand about late static binding, and I saw this answer https://stackoverflow.com/a/35577069/1640606

Which shows the difference as being between those two example:

Note, self::$c

class A
{
    static $c = 7;

    public static function getVal()
    {
        return self::$c;
    }
}

class B extends A
{
    static $c = 8;
}

B::getVal(); // 7

Late static binding, note static::$c

class A
{
    static $c = 7;

    public static function getVal()
    {
        return static::$c;
    }
}

class B extends A
{
    static $c = 8;
}

B::getVal(); // 8

Now, I understand this, but what I don't get is, why the B::getVal(); // 8 first gets the getVal from class A, but seems to get the value defined in class B.

So, ``B::getVal();` is getting the method of the class, but the value of the second class. My question is, is this the real intended purpose of the late static binding and what does it help to resolve

Community
  • 1
  • 1
ʎɹnɔɹǝW
  • 811
  • 2
  • 8
  • 14
  • The answer is in the first sentence of the [documentation](http://php.net/manual/en/language.oop5.late-static-bindings.php) on the PHP manual. – axiac Mar 20 '17 at 15:54
  • Interesting: you wrote: "Now, I understand this, but"... However it is _exactly_ the specific point the example tries to show which you ask about. So... what _did_ you understand of the example you yourself gave? – arkascha Mar 20 '17 at 15:56
  • To answer your own question just ask yourself this: at _run time_, so when ??B::getVal()` is actually called, what does `static::$c` actually resolve to? What would `self::$c` or `$this->c` resolve to instead? – arkascha Mar 20 '17 at 15:57
  • @arkascha well, before I didn't understand what late static binding even meant, or if it was possible parent class could 'abandone' return a child property eventhough the same property exists. But, I am not sure if this 'feature' is what we call late static binding? if so, how does it make sense the parent class to disregard it's own property and return a child property. In classes and traits it is diffierent – ʎɹnɔɹǝW Mar 20 '17 at 15:59
  • You got a wrong approach of how to understand this. The parent class does not "disregard" anything. Actually a class _never_ does anything, it is passive, only methods of _objects_ do something or _functions_ of a static class. Look from the perspective of such a method, here `B::getVal()`: on _which_ class is it called? Right, on `B`. What is the static value of `$c` of _that_ class? Correct! – arkascha Mar 20 '17 at 16:02
  • Hmm, this is confusing, I removed everything 'static' and replaced it with objects (new B)->getVal(); still shows 8. If this can be done without static methods, properties ... then I don't even know why the examples hence the name late 'static' binding was neccessary. – ʎɹnɔɹǝW Mar 20 '17 at 16:08

1 Answers1

1

Example 1:

class A
{
    static $c = 7;

    public static function getVal()
    {
        return self::$c;
    }
}

class B extends A
{
    static $c = 8;
}

B::getVal(); // 7

In Example 1 it is returning you 7 because when you invoke getVal function on B then PHP find its declaration in its parent class which is returning the value of $c from current class using operator self.

Example 2:

class A
{
    static $c = 7;

    public static function getVal()
    {
        return static::$c;
    }
}

class B extends A
{
    static $c = 8;
}

B::getVal(); // 8

But in example 2 when you invoke getVal then again php finds its declaration in its parent class, but class is returning return static::$c which means return the value of the variable of class from which its is invoked.

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • Ah, so `static` means (in this context) "return the value of the variable of class from which it is invoked" as you put it. Hmm, that "static" confused me a lot. It seems if it was '`return origin::$c`' it would've made more sense to me – ʎɹnɔɹǝW Mar 20 '17 at 16:03
  • But if you removed everything 'static' and replaced it with objects `(new B)->getVal();` it still shows 8. If this can be done without static methods, properties ... then why the examples hence the name late 'static' binding? I thought this feature was exclusively with static scope i.e `::` – ʎɹnɔɹǝW Mar 20 '17 at 16:09
  • If you remove all static so, obviously it tries to return the value of variable on which it is invoked. like `$this` do – Sahil Gulati Mar 20 '17 at 16:12
  • but in that case, it should've echoed `7` because `getVal` is defined in class `A` and class `A`s property `$c` has a value of 7 – ʎɹnɔɹǝW Mar 20 '17 at 16:22
  • But you have created object of class B that is why It first tries to fetch the variable value from class B if it is not defined there then it tries from class A like other programming languages – Sahil Gulati Mar 20 '17 at 16:37