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.
Asked
Active
Viewed 1.2k times
29

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 Answers
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

Soliman Mahmoud Soliman
- 302
- 3
- 10
-
Ok, but then what do you with that string that is output???? How do you call a method within it? – Nathan Leggatt Jul 09 '22 at 00:32