Search code examples
phpautoloadsplpsr-0

If PHP libraries can register their own autoloaders, then why does PSR-0 require they be in uniform directories?


I'm building a framework (this is a vast simplification -- please don't recommend using an existing framework instead, that's not helpful) into which I would like to be able to integrate other libraries.

The PSR-0 recommendation suggests that all files within each sub-namespace be contained within their own specific directory. To keep things less complex for users of my framework, I would like to keep everything in one namespace, but organize the files into directories.

If PHP libraries can register their own autoloaders with spl_register_autoload(), then why is it essential that this directory structure be adhered to? Is it feasible/permissable to simply eschew PSR-0, use my own autoloader for my classes, and then use (for example) Symfony's autoloader for any Symfony classes that I may make use of?


Solution

  • There's nobody forcing you to adhere to the standard. Sure, you can make your own autoloader and use it.

    The specification was most likely created in an attempt to make libraries easier to understand, with the idea that if all code uses the same structure, then anyone should be able to instantly know where to go to find a certain class.

    Following standards has certain advantages, such as:

    • People with knowledge of the standard will already know how to use the code
    • Standard compliant tooling can be made to work with adhering code (for example, IDE plugins)
    • Code that assumes standard compliance can already be used with all the adhering code, even if it was not made specifically for your own code (for example, third party autoloaders should be able to load your code and your own autoloader should be able to load third party code)

    A disadvantage might be that you might not like the standard. If that's the case, then you should weigh in the advantages as well when deciding whether or not to follow it.