NotionCommotion Posted March 20, 2019 Share Posted March 20, 2019 I am assuming doing the following is bad practice as the short name "Account" is both for the repository and account. Agree? Appears that use always takes precedent over namespace and just Account\User is always short for \NotionCommotion\Domain\Entity\Account\User and not \NotionCommotion\Api\Account\User. Still sees like it could easily result in confusion. namespace NotionCommotion\Domain\Repository\Account; use NotionCommotion\Domain\Entity\Account; class AccountRepository { public function getUser(string $mainKey, int $userId):?Account\User { // } } Would it be better to do something like the following? Interestingly, it worked fine on the below class but when I tried to do so on the above AccountRepository class, it resulted in AccountDir\User class not found error. Is there any logic reason why that might/should happen, or is it more likely I just have some error elsewhere? namespace NotionCommotion\Api\Account; use NotionCommotion\Domain\Entity\Account as AccountDir; class AccountService { public function read():AccountDir\Account { // } } Quote Link to comment Share on other sites More sharing options...
kicken Posted March 20, 2019 Share Posted March 20, 2019 Is NotionCommotion\Domain\Entity\Account the namespace or the class? Doing use NotionCommotion\Domain\Entity\Account as AccoutDir; class blah extends AccountDir\Account { } Is going to be looking for the class NotionCommotion\Domain\Entity\Account\Account inside namespace NotionCommotion\Domain\Entity\Account which doesn't seem to be correct to me. Assuming the namespace is just up to the entity and account is the class name, then you'd instead just do: use NotionCommotion\Domain\Entity; class blah extends Entity\Account { } Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 21, 2019 Author Share Posted March 21, 2019 19 hours ago, kicken said: Is NotionCommotion\Domain\Entity\Account the namespace or the class? Sorry, should have been more specific. NotionCommotion\Domain\Entity\Account is the namespace, and I have the following files: /www/var/NotionCommotion/Domain/Entity/Account/Account.php /www/var/NotionCommotion/Domain/Entity/Account/User.php /www/var/NotionCommotion/Domain/Repository/Account/Account.php /www/var/NotionCommotion/Api/Account/AccountService.php; Quote Link to comment Share on other sites More sharing options...
kicken Posted March 21, 2019 Share Posted March 21, 2019 In both your samples I would just import the class and alias it if necessary. namespace NotionCommotion\Domain\Repository\Account; use NotionCommotion\Domain\Entity\Account\User; class Account { public function getUser(string $mainKey, int $userId):?User { // } } I don't think the alias is necessary below, but for example. namespace NotionCommotion\Api\Account; use NotionCommotion\Domain\Entity\Account\Account as AccountEntity; class AccountService { public function read():AccountEntity { // } } Quote Link to comment Share on other sites More sharing options...
requinix Posted March 21, 2019 Share Posted March 21, 2019 For the record, unqualified names (no namespace) like "Account" are always class names, so namespace NotionCommotion\Api\Account; use NotionCommotion\Domain\Entity\Account\Account; class AccountService { public function read():Account { // } } is fine. This also works namespace NotionCommotion\Api\Account; use NotionCommotion\Domain\Entity\Account; class AccountService { public function read():Account\Account { // } } (even if it's starting to look weird) because qualified names like "Account\Account" will never overlap with the current namespace, even if they have the same labels, and it uses the normal resolution rules: since "Account" was use-d, Account\Account resolves to NotionCommotion\Domain\Entity\Account\Account, while if it wasn't use-d then it would be NotionCommotion\Api\Account\Account\Account. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 22, 2019 Author Share Posted March 22, 2019 Thank you both, I think the source of my issues is importing namespace and not classes where the namespace final name is the same for multiple namespaces, and as such the following will not work. One option is to rename / www/var/NotionCommotion/Domain/Entity/Account/Account.php to / www/var/NotionCommotion/Domain/Entity/AccountEntity/Account.php, but I think this is confusing as "AccountEntity" sounds like a class and not a namespace. I could rename it to be plural (AccountEntitys), but I am trying to standardize on always being singular. I will probably just use an alias when importing and call it AccountEntity or AccountNS, and at least when looking at the individual class, it is clear what is going on. <?php namespace NotionCommotion\Api\Account; use NotionCommotion\Domain\Entity\Account; use NotionCommotion\Domain\Repository\Account; class AccountService { public function findBy(string $name):Account\Account { return $this->em->getRepository(Account\Account::class)->findBy(['name'=>$name]); } } Quote Link to comment Share on other sites More sharing options...
requinix Posted March 22, 2019 Share Posted March 22, 2019 Alias it. use NotionCommotion\Domain\Entity\Account\Account as EntityAccount; use NotionCommotion\Domain\Repository\Account\Account as RepositoryAccount; Having an "Account" class inside an "Account" namespace is a separate issue, and I would probably promote it to the Entity/Repository namespace. One of the whole points of namespaces is that you don't have to worry about what classes in other namespaces are called, so don't rename the two Accounts merely because they share an otherwise appropriate name. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.