Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 924
ringmaster Avatar asked Dec 03 '12 21:12

ringmaster


People also ask

What PSR 0?

PSR-0 (Autoloading Standard) Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system. Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The _ character has no special meaning in the namespace. The fully-qualified namespace and class are suffixed with .

What is the use of autoload function in PHP?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.


1 Answers

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.

like image 186
rid Avatar answered Sep 25 '22 12:09

rid