为什么:: class附加到Laravel 5.1中的PHP类 [英] Why is ::class appended to PHP class in Laravel 5.1

查看:91
本文介绍了为什么:: class附加到Laravel 5.1中的PHP类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 5.0中,这样的代码用于命名/加载类:

In Laravel 5.0 code like this is used for names-pacing/loading classes:

  'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
         'Illuminate\Foundation\Providers\ArtisanServiceProvider',
         'Illuminate\Auth\AuthServiceProvider',
         'Illuminate\Broadcasting\BroadcastServiceProvider',
         'Illuminate\Bus\BusServiceProvider',
]

但是,在Laravel 5.1中看到了这一点

However, am seeing this in Laravel 5.1

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
         Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
         Illuminate\Auth\AuthServiceProvider::class,
         Illuminate\Broadcasting\BroadcastServiceProvider::class,
         Illuminate\Bus\BusServiceProvider::class,
]

我的问题:Illuminate\Bus\BusServiceProvider::class相对于'Illuminate\Bus\BusServiceProvider'有什么好处,何时应该在类名后附加::class?

My question: What is the benefit of this Illuminate\Bus\BusServiceProvider::class over this 'Illuminate\Bus\BusServiceProvider', when should I append ::class to a class name?

在PHP文档中可以找到什么吗?

Is there any where I can find this in PHP documentation?

推荐答案

此功能已在5.5版中引入,Laravel 5.1现在需要该功能

The feature has been introduced with version 5.5, which is now required by Laravel 5.1

神奇的::class属性包含类的FQN(完全限定名称).

The magic ::class property holds the FQN (fully qualified name) of the class.

它的优点主要来自一个好的IDE.一些是:

The advantages of it mostly comes with a good IDE. Some are:

  • 更少的错字
  • 更轻松的重构
  • 自动完成
  • 点击课程以跳转到文件

有时候,导入类而不是在代码中使用全名也很不错.这使您的代码更整洁,并且所有依赖项都在类顶部用use声明. (我之所以说有时候是因为,对于其中一个类来说,将所有类都导入app.php之类的配置文件中是没有意义的)

Sometimes it's also nice that you can import the class instead of having the full name in the code. This makes your code cleaner and all dependencies are declared with use at the top of the class. (I'm saying sometimes because for one it doesn't make sense to import all classes in a config file like app.php)

这篇关于为什么:: class附加到Laravel 5.1中的PHP类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆