8

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?

Limon Monte
  • 52,539
  • 45
  • 182
  • 213
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96
  • I think it's just a new way to get the class in the newer versions of php – haakym Jun 12 '15 at 10:51
  • like what version precisely ? – Emeka Mbah Jun 12 '15 at 10:53
  • It was mentioned in a video on laracasts, trying to dig it out now as the php docs aren't seeming to yield anything through a google search – haakym Jun 12 '15 at 10:54
  • 1
    okay. I got this `var_dump(Illuminate\Foundation\Providers\ArtisanServiceProvider::class);` `//string 'App\Http\Controllers\Illuminate\Foundation\Providers\ArtisanServiceProvider' (length=75)` – Emeka Mbah Jun 12 '15 at 10:59

1 Answers1

13

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:

  • Fewer 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)

miken32
  • 42,008
  • 16
  • 111
  • 154
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270