0

I use the Slim framework, and I wish I could properly handle self loading files. At the moment I have this:

spl_autoload_register(function($class){
    $list = array(
        dirname(__DIR__).'/app/'.$class.'.php',
        dirname(__DIR__).'/lib/'.$class.'.php',
        dirname(__DIR__).'/lib/'.$class.'.class.php',
    );
    foreach($list as $k => $v){if(file_exists($v)){
        include $v; return true;
    }}
});

And this structure:

lib\
    Function.php
    MyNamespace\
        Class.php
vendor\
    slim\
web\
    .htaccess
    index.php

Is it the best method to autoload Slim without using Composer? (in the case you can't install Composer on your server or you don't want to)

mikmikmik
  • 500
  • 1
  • 5
  • 10
GlaiveÐ
  • 23
  • 1
  • 3

1 Answers1

1

Slim can use Composer to autoload classes. This question more info about using it.

Also the return true; statement in the foreach loop would end the functions execution, without having the whole $list array iterated.

Community
  • 1
  • 1
undefined
  • 2,051
  • 3
  • 26
  • 47
  • Okay, thank you for that answer, but now if I do not want used loader Composer, is there a solution for slim? – GlaiveÐ Jul 19 '14 at 10:05