phppsr-0psr-4psr-2psr-1

PSR-2. When to capitalize directories?


As I understood according to PSR-2 standard we should capitalize directory names which contain classes. But how to do if the directory also contains some other files, for example styles and scripts?

Say I have a plugin directory, each plugin can contain templates and other stuff:

plugins
  PluginName // lowercase?
    PluginName.php // Contains base class "PluginName"
    templates
      home.tpl
    css
      css.css
    js
      js.js
  PluginName2
    ....

Is this structure correct?


Solution

  • You can name your directories whatever you like.

    However, if you intend to autoload your PHP classes with either PSR-0 or PSR-4, the directory names touched by the relevant class name part have to match exactly.

    PSR-2 does not make any assumptions or does not give and rules on which case a classname or namespace has to have.

    PSR-1 does state in chapter 3: "Class names MUST be declared in StudlyCaps." This however only affects the file name of the file containing such class. If you are using PSR-4 autoloading, you can still avoid using any part of the namespace at all in the path if you define a whole map of Name\Space -> directory/for/that/namespace for every directory that contains code.

    PSR-1 does not have any rules about the formatting of the namespace, so you could also avoid using uppercase letters there and because of that avoid having to use uppercase letters in the directory path.

    Note that both PSR-0 and PSR-4 autoloading are case sensitive when it comes to mapping a namespace/classname to a filesystem path and file. So in the end, you will end up having a case sensitive filesystem layout anyways.

    Also note that most namespaces are also using StudlyCaps, and I'd consider using lowercase letters to be unusual.