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.
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.
In PHP, the double colon :: is defined as Scope Resolution Operator. It used when when we want to access constants, properties and methods defined at class level. When referring to these items outside class definition, name of class is used along with scope resolution operator.
Using the ::class keyword in Laravel Since PHP 5.5 the class keyword is used for class name resolution. This means it returns the fully qualified ClassName. This is extremely useful when you have namespaces, because the namespace is part of the returned name.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With