Discussion on: Bootstrapping a CLI PHP application in Vanilla PHP

syntaxseed profile image
SyntaxSeed (Sherri W)

Following this on Termux on my phone & can't get Composer to work.

So here's an autoloader for anyone who needs to do this without composer:

(Include this file into the minicli file.)

 * Example project-specific auto-loading implementation.
 * After registering this autoload function with SPL, the namespaces on the left of the loaders array will load classes found in the paths on the right.
 * @param string $class The fully-qualified class name.
 * @return void

 spl_autoload_register(function ($class) {
    $loaders = [
        'Minicli\\' => '/lib/',
        'App\\' => '/app/'

foreach($loaders as $prefix => $base_dir){
    $len = strlen($prefix);
    if(strncmp($prefix, $class,$len) !== 0){
    $relative_class = substr($class, $len);

    $file = __DIR__ . $base_dir .
       str_replace('\\', '/',
         $relative_class) .

    if (file_exists($file)) {
        require $file;
} //end foreach