Yad*_*ada 88 php class laravel
::classPHP中的符号是什么?
由于语法的性质,快速Google搜索不会返回任何内容.
冒号结肠类
使用这种表示法有什么好处?
protected $commands = [
\App\Console\Commands\Inspire::class,
];
Run Code Online (Sandbox Code Playgroud)
alp*_*yax 74
此功能在PHP 5.5中实现.
文档:http://php.net/manual/en/migration55.new-features.php#migration55.new-features.class-name
它有两个原因非常有用.
use关键字来解析您的类,而无需编写完整的类名.举个例子 :
use \App\Console\Commands\Inspire;
//...
protected $commands = [
Inspire::class, // Equivalent to "App\Console\Commands\Inspire"
];
Run Code Online (Sandbox Code Playgroud)
更新:
此功能对于Late Static Binding也很有用.
__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
Run Code Online (Sandbox Code Playgroud)
xda*_*azz 19
class 是特殊的,由php提供以获得完全限定的类名.
请参见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!
Run Code Online (Sandbox Code Playgroud)
如果你很好奇它属于哪个类别(无论是语言结构等),
这只是一个常数。
PHP 称之为“特殊常量”。它很特别,因为它是由 PHP 在编译时提供的。
特殊的 ::class 常量从 PHP 5.5.0 开始可用,并允许在编译时进行完全限定的类名解析,这对于命名空间类很有用:
https://www.php.net/manual/en/language.oop5.constants.php
| 归档时间: |
|
| 查看次数: |
26959 次 |
| 最近记录: |