简体   繁体   中英

How does name spacing works using PSR4 in laravel 5?

I am a novice laravel developer and I am trying to understand how namespacing works in here. So I was learning about the Repository pattern and decided to go ahead and implement it in my project.

So I created a directory called archive and then loaded it using PSR4 in my composer.json file like so:

"Archive\\": "archive/"

Now I created a Repositories folder in my archive folder where I will be creating all the repositories. And I am namespacing files in it like so:

namespace Archive\Repositories;

Which seems to working fine. Then I created a Contracts folder inside the Repositories folder which will hold all the interfaces to the implementations I am going to use like UserRepositoryInterface for example. And I am namespacing files in the Contracts folder like so:

namespace Archive\Repositories\Contracts;

Which is working fine too.

Now my doubt lies in the concrete implementations I am trying to make in the Repositories folder. Like for example there is a DbUserRepository which implements The UserRepositoryInterface in the Contracts folder.

Now since I am new to this I tried:

class DbUserRepository implements Contacts\UserRepositoryInterface

And it works just fine but then I thought I should use it on top of the file like so:

use Contacts\UserRepositoryInterface;

And I could just do:

class DbUserRepository implements UserRepositoryInterface

And it should work fine according to me but it gives me a class not found exception but when I do something like:

use Archive\Repositories\Contacts\UserRepositoryInterface;

It works fine now. But this is where I am blurry. Inside the DbUserRepository I am in the nampspace of Archive\\Repositories already so why doesn't it just go on to look into the Contracts folder from there? Why do I need to specify the full this as use Archive\\Repositories\\Contacts\\UserRepositoryInterface;

Why can't I just say:

use Contacts\UserRepositoryInterface;

I hope my question is not too confusing. Although my code is working now but I am blurry how namespacing works.

The rules are pretty simple:

  1. All namespace and use statements always use fully qualified names (FQN), meaning they always start from the global namespace and are not relative to anything else. use Foo\\Bar always means \\Foo\\Bar , no matter what namespace you're in.

  2. All literal mentions of names inside the rest of the code are resolved relative to the current namespace and/or aliases established with use statements. new Foo , extends Foo and such either mean __NAMESPACE__\\Foo , or whatever Foo you might have aliased in some use statement.

If you want to shorten names, you need to use use statements which use the FQN of the class, not relative to the current namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM