AutoLoader.class.php 1.16 KB
Newer Older
Zohten's avatar
Zohten committed
1 2
<?php

Zohten's avatar
Zohten committed
3 4 5 6 7
class AutoLoader
{
    public function __construct()
    {
        spl_autoload_register(array($this, 'load'));
Zohten's avatar
Zohten committed
8 9 10 11
        // 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
Zohten's avatar
Zohten committed
12 13 14
    private function load($className)
    {
        if (in_array($className.'.class.php', scandir("model"))) {
Zohten's avatar
Zohten committed
15
            require_once "model/$className.class.php";
Zohten's avatar
Zohten committed
16
            if (is_readable("sql/$className.sql.php")) {
Zohten's avatar
Zohten committed
17
                require_once "sql/$className.sql.php";
Zohten's avatar
Zohten committed
18
            }
Zohten's avatar
Zohten committed
19 20
        }

Zohten's avatar
Zohten committed
21
        if (in_array($className.'.class.php', scandir("classes"))) {
Zohten's avatar
Zohten committed
22
            require_once "classes/$className.class.php";
Zohten's avatar
Zohten committed
23
        }
Zohten's avatar
Zohten committed
24
        
Zohten's avatar
Zohten committed
25
        if (in_array($className.'.class.php', scandir("controller"))) {
Zohten's avatar
Zohten committed
26
            require_once "controller/$className.class.php";
Zohten's avatar
Zohten committed
27
        }
Zohten's avatar
Zohten committed
28 29 30 31 32 33 34 35


        // 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
    }
}

Zohten's avatar
Zohten committed
36
$__LOADER = new AutoLoader();