What does ClassName::class mean in PHP?

前端 未结 3 1899
别跟我提以往
别跟我提以往 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: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
    

提交回复
热议问题