8

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?

ringmaster
  • 2,879
  • 3
  • 28
  • 31
  • 1
    Do what you want. PSR-0 is a recommended standard, use it or not, it's your decision. – vascowhite Dec 03 '12 at 21:51
  • 3
    +1'ed the question. Question is valid and merits an answer imho. – ficuscr Dec 03 '12 at 21:53
  • The PSRs are created by the PHP-Framework Interoperability Group: the answer to your question is in the group name. (Why use `n` autoloaders when there can be one-to-rule-them-all?) – salathe Dec 04 '12 at 08:04
  • 5
    Another question closed prematurely by overzealous moderators. I asked for the reasoning behind the specification, which is a clear fact if someone knows it -- I don't. I asked whether it's feasible (or maybe "advisable" would have been a better word) to ignore the specification, which would be something that answerers could adequately explain, and have! Moreover, I don't see any arguing here. This closing simply seems like people not wanting a question to be asked. – ringmaster Dec 07 '12 at 17:33

2 Answers2

6

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.

rid
  • 61,078
  • 31
  • 152
  • 193
1

In addition, if other libraries adhere to the standard, you can just use a single autoloader to load classes from multiple libraries, rather than use your autoloader for your classes, and then have to load Symfony's autoloader for its classes and then library X's autoloader for its classes.

As long as the autoloader is PSR compliant and the other library's directory structures are too, you can stick to using one autoloader.

drew010
  • 68,777
  • 11
  • 134
  • 162