9

For example Symfony uses \Controller. Yii2 uses \controllers and \models.

Is there a standard about ...s|es like PSR?

Ivan
  • 625
  • 1
  • 5
  • 18
  • 1
    Once I seeked for this info time ago, I came across an answer (I don't have the link) that said: If the container contains all of the elements exactly of the same type, name if with "s". For example use `Exceptions` if all the classes inside there are exactly exception. Instead use singular if the things inside are "related to", for example use `Exception` if inside you have both `MyNiceException` and in addition an `ExceptionFactory` for example. I used that guideline since a few years ago and I feel comfortable. That is far of being an standard, but works easing readability. – Xavi Montero May 06 '19 at 17:48
  • Instead, I don't change the framework defaults. For example in Symfony I don't force `Controllers`, I just keep `Controller` to do not break anything. I do that in the previous comment only for the namespaces I create on my own. For the `Model` in particular I use singular as I have there entities, repositories, writers, readers, services, etc... I don't have 200 "models" in my project I have "one model" with "many things inside". – Xavi Montero May 06 '19 at 17:52
  • This answer https://softwareengineering.stackexchange.com/a/75929/150005 seems to reinforce this theory. – Xavi Montero May 06 '19 at 17:53

1 Answers1

13

None of the PSRs specify whether to use singular or plural in namespaces. This is usually a convention by the framework. The same goes for capitalization.

Whether you can use your preferred naming convention - if you have any - in the framework of your choosing mostly depends on the framework itself and might even vary within parts of the framework. In the case of Symfony it is singular App\Controller by default, but it's easily changed to App\Controllers or App\controllers if you like as long as your routing configuration uses the correct name.

When deviating from the framework's convention you might run into problems, because of the expectations it has when configuring your app. For example Symfony 3 autoloads your Commands when they are placed in the Command/ folder and the class name is suffixed with Command. When you deviate from this you have to manually register them. On the other hand Symfony 4 uses the new autiwiring and autoconfigure defaults in the service configuration and will detect commands based on whether they implement the correct interface - either directly or indirectly by extending the base Command-class - and therefore can have whatever name you like and can be put in any folder. So even within the framework or between different versions of the framework changing the name to something other than the recommendation might have different repercussions.

dbrumann
  • 16,803
  • 2
  • 42
  • 58