PHP的 类名::class 是啥?
This feature was implemented in PHP 5.5.Documentation :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 theu
sek
eyword to resolve your class and you don't need to write the full class name.
For exemple :
use \App\Console\Commands\Inspire;
//...
protected $commands = [
Inspire::class, // Equivalent to "App\Console\Commands\Inspire"
];
Update:This feature is also useful forLate Static Binding.Instead of using the__CLASS__mag
ic constant, you can use thestatic::classfeatu
re to get the name of the derived class inside the parent class. For example:
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
来源:https://stackoverflow.com/questions/30770148/what-is-class-in-php
热门话题 · · · · · · ( 去话题广场 )
- 解锁我的夏日旅行足迹地图 活动 24.4万次浏览
- 你想对高考生们说点什么? 2.7万次浏览
- 每天夸夸自己的三件小事 3666次浏览
- 第26届上海国际电影节 8.5万次浏览
- 晒出你的上影节最爱 新话题
- 遗落在草稿箱的过期日常 476次浏览