I have a tiny application that i need an autoloader for. I could easily use the symfony2 class loader but it seems like overkill.
Is there a stable extremely lightweight psr-0 autloader out there?
I have a tiny application that i need an autoloader for. I could easily use the symfony2 class loader but it seems like overkill.
Is there a stable extremely lightweight psr-0 autloader out there?
The doctrine classloader is another good choice. You can easily install it with composer
The PSR-0 specification document has an examplary compatible autoloader function that is already pretty short:
It's usage is pretty straight forward:
The shortcoming with it is, that you need to configure base-directories it works on with the
include_path
directive. To support a hybrid PSR-0 autoloaders leaned onto SPL semantics, the following one supportss include path and spl autoload extensions:The The Symfony2 ClassLoader Component has the benefit to allow more configuration here. You can install it easily via Pear or Composer (symfony/class-loader on Packagist). It is a component on it's own that is used by many and fairly well tested and supported.
An exact equivalent of the answer @hakre provided, just shorter:
You can also set the base directory by changing
$path = null;
to another value, or just do like this:This is not a direct answer to the question, but I found that the above answers worked great on standalone PHP scripts, but were causing issues when used in certain frameworks, such as Joomla.
For anyone using Joomla, it turns out that there is a compatible autoloader already built into the framework, therefore you won't need to use the above functions. In that instance, just call JLoader::registerNamespace().... for example:
This even supports things like: $transformerContstraint = new \Recurr\Transformer\Constraint\AfterConstraint(new DateTime());
Just put it in /vendor/Recurr/Transformer/Constraint/AfterConstraint.php
SplClassLoader seems like a right choice. It's an implementation proposed by PSR-0 itself.