1

The PHP docs explain about the Scope Resolution Operator (::) that it is for use mainly in static contexts along with self, static and parent.

But then you find that it's possible to use it instead of $this regardless static context or not, as in this example:

class A {
   public function funA() {
      self::funB();  // *Works*
      static::funB();   // *Works*
      $this->funB();  // *Works*
   }
   public function funB() {
   }
}

Is this a PHP design issue or am I misunderstandig something? I find the use of ::, static, and self a little confusing... When to choose one or other?

yivi
  • 42,438
  • 18
  • 116
  • 138
oxk4r
  • 452
  • 6
  • 17
  • 1
    https://stackoverflow.com/a/151976/9921075 I suggest you look at this answer :) – Eza bilam Jul 21 '18 at 16:39
  • @Ezabilam but then some of the options should thow an error depending on the context, because it leads to confusion... – oxk4r Jul 21 '18 at 16:50
  • Possible duplicate of [When to use self over $this?](https://stackoverflow.com/questions/151969/when-to-use-self-over-this) – yivi Jul 22 '18 at 09:12

1 Answers1

1

PHP Manual explained this in Late Static Bindings although perhaps it is not as concise as it should be. Allow me to offer my own explanation.

Here is the guideline:

  • self always refers to the class where the word self appears.
  • static always refers to the current class in static context, even if the class is extended.
  • $this always refers to the current class in object context, even if the class is extended.

As shown in the following example, use self if you want to always refer to class A, use static and $this if you want to always refer to class B, when B is the current class.

class A {
    public function test() {
        echo self::who();   // always class A
        echo static::who(); // always the current class (static context)
        echo $this->who();  // always the current class (object context)
    }
    public function who() {
        echo __CLASS__ . "\n";
    }
}

class B extends A {
    public function who() {
        echo __CLASS__ . "\n";
    }
}

(new B)->test();

Output:

A
B
B

As you can see, static and $this refer to the same class (class B). Let's extend the example with another class that extends class B:

class C extends B {
    public function who() {
        echo __CLASS__ . "\n";
    }
}

(new C)->test();

Output:

A
C
C

Notice that static and $this refer to the same class again, class C this time, because static and $this always refer to the current class, even if the class is extended.

So what is the difference between static and $this? The difference is the context. $this requires an object (a class instance), while static does not.

Let's see what happens if test() is called in static context:

C::test();

Output:

A
C

Fatal error: Using $this when not in object context in C:\wamp\www\test.php on line 7

The first and the second calls work fine in static context but the third call fails because $this requires object context. See the code in action at https://ideone.com/AVtITz

Rei
  • 6,263
  • 14
  • 28