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?
Why use a PSR-0 or PSR-4 autoload in composer if classmap is actually faster?
Because it's more practical.
In production, you can use a classmap (with composer dumpautoload -o
) because you won't add any new class, but in dev environment it's interesting to have the flexibility provided by PSR-0 or PSR-4 (i.e. nothing to do when adding new classes).
Update: you can also use composer install -o
, it's simpler.
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);
}
}
What does PHP do when working out the connection between the use of the use statement and a class it is supposed to be for?
The use statement doesn't actually load the namespace/class into your file. It simply sets up a list of aliases to refer to the classes in that namespace.
When it encounters a class that hasn't yet been declared, it uses that list of aliases to try to fully qualify the class name (prefix replacement). If it can't find an alias for the class, it'll use the namespace of the current scope to qualify the class name.
Only when the class name is fully qualified, that php will try to autoload the class (calling the various autoloaders that might have been defined).
Are PSR-0/PSR-4 autoloaders affected in the way they work when it comes to these two code samples?
No, autoloaders are not affected in the way they work by the difference in your code samples because php will call the autoloaders exactly the same way with exactly the same parameters.
You can autoload specific files by editing your composer.json
file like this:
"autoload": {
"files": ["src/helpers.php"]
}
(thanks Kint)
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.