Monday, March 7, 2022

[FIXED] Any difference between this 2 ways to use a class in PHP?

Issue

Currently developing a Laravel app and a question came to mind. Is any "technical" difference between this two ways to use a class?

First way:

public function store() {
    new \Illuminate\Auth\Events\Registered($user)
}

VS

Second way:

use Illuminate\Auth\Events\Registered;

public function store() {
    new Registered($user)
}
  1. One way is more performant than the other?
  2. Is any difference in the way PHP reads this lines?
  3. When the class is only used once, is better to include it using first way or second way?
  4. Also any other this than comes to mind...

Solution

No difference in performance but its personal choice but if you are using the class more than once in the same class importing once works well and looks clean in my view



Answered By - Nishan Timsina

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.