Viewed   70 times

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 ask extremely lightweight, let's do so ;)

Timothy Boronczyk wrote a nice minimal SPL autoloader :

I condensed the code like this:

function autoload1( $class ) {
    preg_match('/^(.+)?([^\\]+)$/U', ltrim( $class, '\' ), $match ) );
    require str_replace( '\', '/', $match[ 1 ] )
        . str_replace( [ '\', '_' ], '/', $match[ 2 ] )
        . '.php';

Then compare (minified versions of) this [autoload3] with short @Alix Axel code [autoload4] :

function autoload3($c){preg_match('/^(.+)?([^\\]+)$/U',ltrim($c,'\'),$m);require str_replace('\','/',$m[1]).str_replace(['\','_'],'/',$m[2]).'.php';}
function autoload4($c){require (($n=strrpos($c=ltrim($c,'\'),'\'))!==false?str_replace('\','/',substr($c,0,++$n)):null).str_replace('_','/',substr($c,$n)).'.php';}

autoload3 is the shortest !

Let's use stable & extremely lightweight (175b !) autoloader file :

<?php spl_autoload_register(function ($c){preg_match('/^(.+)?([^\\]+)$/U',ltrim($c,'\'),$m);require str_replace('\','/',$m[1]).str_replace(['\','_'],'/',$m[2]).'.php';});

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...).

<?php spl_autoload_register(function($c){@include preg_replace('#\|_(?!.+\)#','/',$c).'.php';});

If you want to make it shorter / better, please use this gist.

Sunday, December 4, 2022

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.

Monday, December 12, 2022

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);
Saturday, September 17, 2022

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.

Thursday, September 15, 2022

You can autoload specific files by editing your composer.json file like this:

"autoload": {
    "files": ["src/helpers.php"]

(thanks Kint)

Friday, September 16, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :