laravel 在 PHP 中 ClassName::class 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32448481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does ClassName::class mean in PHP?
提问by yinwei
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.
当我阅读用 Laravel 框架编写的代码时,我看到了很多ClassName::class
. 修饰符是什么意思::class
?这是 Laravel 或 PHP 的特性吗?我已经搜索过,但找不到文档。
回答by DeDee
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 ClassNameclass by using
ClassName::class
. This is particularly useful with namespaced classes.
自 PHP 5.5 起,class 关键字也用于类名解析。你可以得到包含的完全限定名称的字符串类名使用类
ClassName::class
。这对于命名空间类特别有用。
For example
例如
namespace MyProject;
class Alpha{ }
namespace MyOtherProject;
class Beta{ }
echo Alpha::class; // displays: MyProject\Alpha
echo Beta::class; // displays: MyOtherProject\Beta
回答by Soliman Mahmoud Soliman
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.
它只返回带有命名空间的类名!自 PHP 5.5 起,class 关键字也用于类名解析。您可以使用 ClassName::class 获取包含 ClassName 类的完全限定名称的字符串。这对于命名空间类特别有用。
namespace NS {
class ClassName {
}
echo ClassName::class;
}
The above example will output:
上面的例子将输出:
NS\ClassName