Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cragthehack's avatar

Laravel 5.1: What is "::class,"?

I'm fairly new to PHP. I know what "::" is. But i see statements like

App|User::class,

can someone explain to me (or point me to some docs) what the "::class," is? I've never seen that before. Thanks.

UPDATE: Never mind. I got it. And I upgraded my PHP to 5.5. (is there a way to delete forum posts?)

0 likes
9 replies
jekinney's avatar

Using that ::class syntax allows you to declare a class without passing it as a string. When they update ide's it will help to auto fill namespaces which can't be done with a string.

2 likes
absiddiqueLive's avatar
Level 7

@cragthehack It just return class name with namespace !
Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes.

namespace NS {
    class ClassName {
    }
    echo ClassName::class;
}

The above example will output:

NS\ClassName
1 like
bobbybouwmann's avatar

So as an example

User::class == 'App\User'

AppServiceProvider::class == 'App\Providers\AppServiceProvider'
Qlic's avatar

What are your thoughts on performance, since it seems like this is static calling? What if you use this ::class throughout the application, lets say hundreds of times, instead of hardcoding classnames in strings? Would this have significant impact?

pmall's avatar

@cragthehack note this is a php feature (> version 5.5 I guess), not a laravel feature.

What are your thoughts on performance, since it seems like this is static calling? What if you use this ::class throughout the application, lets say hundreds of times, instead of hardcoding classnames in strings? Would this have significant impact?

There is no impact, stop overthinking.

3 likes
Nick385's avatar

I was wondering the same thing, and found a well formulated explanation:

When you have ::class in your code, what actually happens is that PHP takes the name of the class you've specified and generates the fully-qualified namespaced classname as a string. So yes, this does indeed mean that you're getting the computer to do more work than if you'd just supplied a string.

However, it does this at compile time. This means that it will only ever do it once, and it happens outside of the bounds of your program runtime. Therefore your Benchmark performance timer won't take it into account -- in fact when your code runs, it just sees a string, which means that what you're actually benchmarking is the program doing virtually exactly the same thing in both scenarios. Any difference in timing between the two is an artifact and can be ignored.

The other important point about it being done at compile time is that if you have OpCache enabled then it will only ever have to convert the string once, the first time you run it, which means that there's even less of a difference between the two.

The ::class syntax wasn't added to the language for performance reasons; it was added because it helps with code quality, readability and maintainability. And frankly, those factors are almost always far more important than micro-optimised performance.

Further clarification on bencharking

Since it's done at compile time, you can't get an accurate benchmark for it from inside of the PHP program.

If you really want to benchmark it, your best option would be to write a shell script with a timer and call the program from there. You'll need to have a separate PHP program for each variation. But even then, any benchmark results you get will be somewhat artificial and unlikely to be useful in any meaningful way. You'll get different results depending on whether you run multiple loops in a single instance of the program or have the loop in the shell script. You'll also get different results if you have OpCache enabled.

But ultimately, you really shouldn't even be thinking about whether to benchmark this -- it's a syntax nicety; it doesn't affect performance. Or if it does, the effect is so tiny that it's not worth thinking about.

The secret of good performance tuning is to find the biggest bottlenecks in your code, and deal with them first. Not the smallest.

Source: https://stackoverflow.com/questions/32630331/php-class-name-constant-vs-string-perfomance

vikrantsingh47's avatar

what exactly is "compile time" in laravel? when does laravel compiles code? i thought it just takes a http request and route it to a controller and sends the response...

Please or to participate in this conversation.