In Laravel 5.1, the kernel for the CLI class looks something like this
#File: app/Console/Kernel.php
class Kernel extends ConsoleKernel
{
//...
protected $commands = [
\App\Console\Commands\Inspire::class,
];
//...
}
Is the change to using the predefined/magic constant ::class
\App\Console\Commands\Inspire::class
functionally different than simply using the class name?
\App\Console\Commands\Inspire
Solved undefined constant error in laravel view using single quotes. You have to pass a value with a single quote in the method. This error occurs because laravel treats it as a constant name because you did not pass the value to the method with single or double quotes.
A class constant is a field that is declared with the static and final keywords.
Using the ::class keyword in Laravel This means it returns the fully qualified ClassName. This is extremely useful when you have namespaces, because the namespace is part of the returned name.
A class constant is declared inside a class with the const keyword.
Nope, using ::class
on a class returns the fully qualified class name, so it's the same thing as writing 'App\Console\Commands\Inspire'
(in quotes, since it's a string). The class
keyword is new to PHP 5.5.
It looks silly in this example, but it can be useful in e.g. testing or in defining relations. For instance, if I have an Article
class and an ArticleComment
class, I might end up doing
use Some\Long\Namespace\ArticleComment;
class Article extends Model {
public function comments()
{
return $this->hasMany(ArticleComment::class);
}
}
Reference: PHP Docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With