PHP 中的 ::class 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30770148/
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 is ::class in PHP?
提问by Yada
What is the ::class
notation in PHP?
::class
PHP 中的符号是什么?
A quick Google search returns nothing because of the nature of the syntax.
由于语法的性质,快速的 Google 搜索不会返回任何内容。
colon colon class
冒号类
What's the advantage of using this notation?
使用这个符号有什么好处?
protected $commands = [
\App\Console\Commands\Inspire::class,
];
回答by alphayax
This feature was implemented in PHP 5.5.
这个特性是在 PHP 5.5 中实现的。
Documentation : http://php.net/manual/en/migration55.new-features.php#migration55.new-features.class-name
文档:http: //php.net/manual/en/migration55.new-features.php#migration55.new-features.class-name
It's very useful for 2 reasons.
它非常有用有两个原因。
- You don't have to store your class names in strings anymore. So, many IDEs can retrieve these class names when you refactor your code
- You can use the
use
keyword to resolve your class and you don't need to write the full class name.
- 您不必再将类名存储在字符串中。因此,当您重构代码时,许多 IDE 可以检索这些类名
- 您可以使用
use
关键字来解析您的类,而无需编写完整的类名。
For exemple :
举个例子 :
use \App\Console\Commands\Inspire;
//...
protected $commands = [
Inspire::class, // Equivalent to "App\Console\Commands\Inspire"
];
Update:
更新:
This feature is also useful for Late Static Binding.
此功能对于后期静态绑定也很有用。
Instead of using the __CLASS__
magic constant, you can use the static::class
feature to get the name of the derived class inside the parent class. For example:
__CLASS__
您可以使用该static::class
功能来获取父类中派生类的名称,而不是使用魔术常量。例如:
class A {
public function getClassName(){
return __CLASS__;
}
public function getRealClassName() {
return static::class;
}
}
class B extends A {}
$a = new A;
$b = new B;
echo $a->getClassName(); // A
echo $a->getRealClassName(); // A
echo $b->getClassName(); // A
echo $b->getRealClassName(); // B
回答by xdazz
class
is special, which is provided by php to get the fully qualified class name.
class
是特殊的,由 php 提供,用于获取完全限定的类名。
See http://php.net/manual/en/migration55.new-features.php#migration55.new-features.class-name.
请参阅http://php.net/manual/en/migration55.new-features.php#migration55.new-features.class-name。
<?php
class foo {
const test = 'foobar!';
}
echo foo::test; // print foobar!
回答by Lucas Bustamante
If you're curious in which category ::class
falls into, (whether it's a language construct, etc), it's very simple. It's a constant. PHP calls it a "Special Constant". It's special because it's provided by PHP.
如果您想知道::class
属于哪个类别(无论是语言结构等),这很简单。这是一个常数。PHP 称之为“特殊常量”。它很特别,因为它是由 PHP 提供的。
回答by Harald Witt
Please be aware to use the following:
请注意使用以下内容:
if ($whatever instanceof static::class) {...}
This will throw a syntax error:
这将引发语法错误:
unexpected 'class' (T_CLASS), expecting variable (T_VARIABLE) or '$'
unexpected 'class' (T_CLASS), expecting variable (T_VARIABLE) or '$'
But you can do the following instead:
但您可以改为执行以下操作:
if ($whatever instanceof static) {...}
or
或者
$class = static::class;
if ($whatever instanceof $class) {...}