Using the :: operator on an object

I recently found an interesting use of code, and I did not know that this was possible. Can someone explain or give me a help page explaining why the code below works? I understand what ::can be used to reflect parent, static, etc. Or for accessing static fields / methods, but with a link $thisseems weird mostly because the method is a()not static

class Test
{
   private function a()
   {
      echo 'a works';
   }

   public static function c()
   {
      echo 'c works';
   }

   public function b()
   {
        $this::a(); // this is weird
        $this::c(); // also this
        $this->a(); // normal usage
        self::a();  // as expected
        static::a(); // same as above
        Test::c();  // as expected
   }
}


(new Test)->b();

I tried to find some information myself, but no luck.

Edit:

I know that ::I also know that it will give a warning if E_STRICT is enabled.

+4
source share
1 answer

PHP 5.3, ::. , , ; , , , . , static: http://php.net/manual/en/language.oop5.static.php#language.oop5.static.properties.

, :

$foo = new Foo;
$foo::bar();

$foo = 'Foo';
$foo::bar();

Foo::bar();

; , static , , E_STRICT.

, , , ; , - , . . :.

$foo = new SomeClassWithAVeryLongName;
$foo->bar($foo::BAZ); // much more concise than repeating SomeClassWithAVeryLongName::
+4

All Articles