Using ::class static class keyword in PHP
If you’ve worked with modern frameworks such as Laravel, you might have seen this keyword
::class be used extensively throughout the application.
So, for instance, in Laravel, in
app/Http/Kernel.php, there’s a property
$middleware which contains all the middlewares that the application is using. Here’s how the property looks like.
protected $middleware = [
So, what does the
::class keyword exactly do in this case? Well, when you append the
::class keyword to any class, PHP will resolve it into a string which contains the fully qualified name of the class.
So, the following,
is equivalent to…
One should use the
::class instead of using the fully qualified name as a string, as it can help in navigating to the class from the IDE such as PhpStorm right from where it’s defined.
Apart from this, it helps in reducing the human error while trying to write the fully qualified class names as a string as you just need to import the class which is again very easy in PhpStorm.
Like this article? Consider leaving aTip