AutoLoader.class.php 1.12 KB
Newer Older
Quentin Vrel's avatar
Quentin Vrel committed
1 2 3 4 5 6 7 8 9 10 11 12
<?php

class AutoLoader {
    

    public function __construct() {
        spl_autoload_register( array($this, 'load') );
        // spl_autoload_register(array($this, 'loadComplete'));
    }

    // This method will be automatically executed by PHP whenever it encounters an unknown class name in the source code
    private function load($className) {
13
        if(in_array($className.'.class.php', scandir("model"))){
Quentin Vrel's avatar
Quentin Vrel committed
14 15 16 17 18
            require_once "model/$className.class.php";
            if (is_readable("sql/$className.sql.php")) 
                require_once "sql/$className.sql.php";
        }

19
        if (in_array($className.'.class.php', scandir("classes"))) 
Quentin Vrel's avatar
Quentin Vrel committed
20 21
            require_once "classes/$className.class.php";
        
22
        if (in_array($className.'.class.php', scandir("controller"))) 
Quentin Vrel's avatar
Quentin Vrel committed
23 24
            require_once "controller/$className.class.php";

25

Quentin Vrel's avatar
Quentin Vrel committed
26 27 28 29 30 31 32 33
        // TODO : compute path of the file to load (cf. PHP function is_readable)
        // it is in one of these subdirectory '/classes/', '/model/', '/controller/'
        // if it is a model, load its sql queries file too in sql/ directory

    }
}

$__LOADER = new AutoLoader();