Why is ::class appended to PHP class in Laravel 5.1

半城伤御伤魂 提交于 2020-01-02 02:20:32

问题


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',
]

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,
]

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?

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


回答1:


PHP Documentation on ::class

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

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

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

  • Less typos
  • Easier Refactoring
  • Auto-Completion
  • Click on class to jump to the file

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)



来源:https://stackoverflow.com/questions/30801128/why-is-class-appended-to-php-class-in-laravel-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!