A method which is declared as static
will always be called statically:
public static function bar() { var_dump($this); }
Whichever way you call this method, it will result in:
Fatal error: Uncaught Error: Using $this when not in object context
(Or variations thereof depending on your version of PHP.)
A function which does not have the static
keyword behaves… differently:
class Foo {
public function bar() { var_dump($this); }
}
$f = new Foo;
$f::bar();
Deprecated: Non-static method Foo::bar() should not be called statically
Fatal error: Uncaught Error: Using $this when not in object context
Calling the function from outside actually calls it statically.
class Foo {
public function bar() { var_dump($this); }
public function baz() { $this::bar(); }
}
$f = new Foo;
$f->baz();
object(Foo)#1 (0) {
}
Calling the function via $this::
calls it in an object context.
The manual only has these vague paragraphs to offer for the ::
operator:
When referencing these items from outside the class definition, use the name of the class.
As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).
It appears that outside a class ::
always operates statically, and $f::
substitutes the name of the class. However, within an object context, ::
preserves the context, likely to enable correct operation of parent::
calls, which obviously should preserve the object context.