AutoLoader.class.php 1.08 KB
Newer Older
thibaut-felten's avatar
thibaut-felten committed
1 2
<?php

3 4
class AutoLoader {

5 6 7 8
    public function __construct() {
        spl_autoload_register( array($this, 'load') );
        // spl_autoload_register(array($this, 'loadComplete'));
    }
9

10 11 12 13 14
    // This method will be automatically executed by PHP whenever it encounters an unknown class name in the source code
    private function load($className) {
        $paths = array('/classes/','/model/','/controller/');
        $fileToLoad = null;
        $i = 0;
thibaut-felten's avatar
thibaut-felten committed
15

16 17 18 19 20
        do{
            $fileToLoad = __ROOT_DIR . $paths[$i] . ucfirst($className
            ) . '.class.php';
            $i++;
        } while(!is_readable($fileToLoad) && $i<count($paths));
thibaut-felten's avatar
thibaut-felten committed
21

22 23 24 25 26
        if(! is_readable($fileToLoad)){
            throw new Exception('Unknown class '.$className);
        }
        
        require_once($fileToLoad);
thibaut-felten's avatar
thibaut-felten committed
27

28 29 30 31 32
        if(strlen(strstr($fileToLoad,'/model/'))>0){
            $sqlFileToLoad = __ROOT_DIR . '/sql/' . ucfirst($className) . '.sql.php';
            if(is_readable($sqlFileToLoad)){
                require_once($sqlFileToLoad);
            }
thibaut-felten's avatar
thibaut-felten committed
33
        }
thibaut-felten's avatar
thibaut-felten committed
34
    }
35
}
thibaut-felten's avatar
thibaut-felten committed
36

37
$__LOADER = new AutoLoader();