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?
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 .
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With