Asked  7 Months ago    Answers:  5   Viewed   33 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.

Wednesday, March 31, 2021
answered 7 Months ago

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/');

If you decide to use Composer as your vendor manager then add your custom namespaces to your composer.json and include vendor/autoload.php

Wednesday, March 31, 2021
answered 7 Months ago

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);
Wednesday, March 31, 2021
answered 7 Months ago

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

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

(thanks Kint)

Saturday, July 3, 2021
answered 4 Months ago

There are two ways to fix it.

  1. change composer.json to

    "Portal\Core\": "src/core/"
  2. Or rename the core directory to Core

The subdirectory name MUST match the case of the sub-namespace names.

Wednesday, August 11, 2021
Philip Weiser
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :