29

When I read code written in the Laravel framework, I see a lot of uses of ClassName::class. What does is the meaning of the modifier ::class? Is this a feature of Laravel or PHP? I've searched but been unable to find documentation.

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
yinwei
  • 331
  • 1
  • 3
  • 4
  • 1
    [This](http://php.net/manual/en/language.oop5.basic.php) link takes you to the correct page in the PHP docs that explains class name resolution. – alscxc Apr 17 '18 at 00:53

3 Answers3

42

Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes.

For example

namespace MyProject;
class Alpha{ }

namespace MyOtherProject;
class Beta{ }

echo Alpha::class; // displays: MyProject\Alpha
echo Beta::class; // displays: MyOtherProject\Beta
DeDee
  • 1,972
  • 21
  • 30
12

It just returns the class name with namespace! Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes.

namespace NS {
    class ClassName {
    }
    echo ClassName::class;
}

The above example will output:

NS\ClassName
DaFunkyAlex
  • 1,859
  • 2
  • 24
  • 36
-1

Please refer to this

::class
Since PHP 5.5, the class keyword is also used for class name resolution. 
evinlort
  • 17
  • 3