Differences between ways to reference a namespace

1

I'd like to understand why in some code examples, people import classes as follows:

use \Firebase\JWT\JWT;

and not just:

use Firebase\JWT\JWT;

Is there any difference in starting with a \ "?

In my tests for my classes the result was exactly the same for both forms of use. So I wonder if there's a difference or it's just optional.

    
asked by anonymous 13.04.2018 / 21:48

1 answer

2

In this case there is no difference.

But imagine the following scenario:

You have class App\User and class Services\User .

namespace App\User;

public function teste() {
   $services = new Services\User;
}

This example would cause an error since it would look at App\User\Services\User

If the slash was placed before the class would be found, it would use \Services as the root.

When we use use it disregards current namespace , not needing to use \ .

    
13.04.2018 / 22:15