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?
You need include (include/require statement) the file with the autoloader code in the first script of your application
If you choose for use the Composer's autoloader as @Skpd said then you should have a code like this in the top of your first PHP script.
include_once __DIR__ . '/composer_autoloader.php'
$loader = new ComposerAutoloadClassLoader();
$loader->add('MyLib', __DIR__.'/library/');
$loader->register();
If you decide to use Composer as your vendor manager then add your custom namespaces to your composer.json
and include vendor/autoload.php
This is because you are in the TactViewManager
namespace.
The pseudo-namespaced classes are in fact in the global namespace,
so you should prefix them with to call them:
$loader = new Twig_Loader_Filesystem($this->templatepath);
If the prefix bugs you, you could do this:
namespace TactViewManager;
use Twig_Loader_Filesystem;
use Twig_Environment;
class ViewManager {
public function init()
{
$loader = new Twig_Loader_Filesystem($this->templatepath);
$this->twig = new Twig_Environment($loader);
}
}
You can autoload specific files by editing your composer.json
file like this:
"autoload": {
"files": ["src/helpers.php"]
}
(thanks Kint)
There are two ways to fix it.
change composer.json
to
"Portal\Core\": "src/core/"
Or rename the core
directory to Core
https://getcomposer.org/doc/04-schema.md#psr-4
The subdirectory name MUST match the case of the sub-namespace names.
http://www.php-fig.org/psr/psr-4/
You ask extremely lightweight, let's do so ;)
Timothy Boronczyk wrote a nice minimal SPL autoloader : http://zaemis.blogspot.fr/2012/05/writing-minimal-psr-0-autoloader.html
I condensed the code like this:
Then compare (minified versions of) this [autoload3] with short @Alix Axel code [autoload4] :
autoload3 is the shortest !
Let's use stable & extremely lightweight (175b !) autoloader file :
Maybe i'm crazy but you Asked for extreme, no?
EDIT: Thanks to Alix Axel, i've shorten the code (only 100b !) and used include instead of require in case you have various autoloading strategy for old libs (and then various autoloader in spl autoload stack...).
If you want to make it shorter / better, please use this gist.