What does ClassName::class mean in PHP?

前端 未结 3 1874
别跟我提以往
别跟我提以往 2020-12-31 00:57

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 th

相关标签:
3条回答
  • 2020-12-31 01:31

    Please refer to this

    ::class
    Since PHP 5.5, the class keyword is also used for class name resolution. 
    
    0 讨论(0)
  • 2020-12-31 01:39

    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
    
    0 讨论(0)
  • 2020-12-31 01: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
    
    0 讨论(0)
提交回复
热议问题