1

The documentation says that:

This feature was named "late static bindings" with an internal perspective in mind. "Late binding" comes from the fact that static:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information. It was also called a "static binding" as it can be used for (but is not limited to) static method calls.

What does it mean by "not limited to static methods only"?

I'm just trying to understand that, I don't want to do something concrete, an example would be nice, for example.

jwa
  • 122
  • 1
  • 7

1 Answers1

3

The place I most commonly use it is when you want to override a constant in an inheriting class, but it's used in a method that's not also overridden in the inheriting class:

class A
{
    const FOO = 'A';
    public function foo()
    {
        printf("I am: %s\n", get_class($this));
        printf("self::FOO = %s\n", self::FOO);
        printf("static::FOO = %s\n", static::FOO);
        echo "\n";
    }
}

class B extends A
{
    const FOO = 'B';
}

Here, if we run the inherited method that uses self::, we'll get the value that's defined in the parent class, not the instantiated class. I.e., inside the B class, self::FOO has a value of A if it's referred to by a method inherited from A. So self:: will give us the class where the method is defined, and static:: will give us the class we happen to be in at runtime.

(new A())->foo();
(new B())->foo();

Outputs:

I am: A
self::FOO = A
static::FOO = A

I am: B
self::FOO = A
static::FOO = B

So this is not a static method call, but we're still using the static:: keyword to make sure we get the overriding value, if any.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • HI @Alex Howansky, sorry for the late reply, I just got into SO. Excellent example, so, the documentation references that we can use `static::` **inside** non-static methods, right? – jwa Jun 27 '23 at 21:04
  • I'm not sure if the documentation references it, but you definitely can. – Alex Howansky Jun 28 '23 at 22:04
  • Ok, anyway your example is a good one @Alex Howansky – jwa Jun 29 '23 at 11:48