Differences between ways to reference a namespace

Asked

Viewed 53 times

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 the two forms of use. So I’d like to know if there really is a difference or is it just optional.

1 answer

2


In that case there is no difference.

But imagine the following scenario:

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

namespace App\User;

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

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

Already if the bar was placed before the class would be found, as it would use \Services as the root.

When we use the use he disregards the namespace current, it is not necessary to use the \.

  • Yes that I understand. The problem is that I usually see this against "" at the beginning of "use" itself. That doesn’t make much sense to me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.