<?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) {
            $paths = array('/classes/', '/controller/', '/model/', '/sql/');
            $fileToLoad = null;
            $i = 0;

            do {
                $fileToLoad = __ROOT_DIR . $paths[$i] . ucfirst($className) . '.class.php';
                $i++;
            } while(!is_readable($fileToLoad) && $i < count($paths));

            if (!is_readable($fileToLoad))
                throw new Exception('Unknown class ' . $className);

            require_once($fileToLoad);

            if (strlen(strstr($fileToLoad, '/model/')) > 0) {
                $fileToLoadSql = __ROOT_DIR . '/sql/' . ucfirst($className) . '.sql.php';

                if (is_readable($fileToLoadSql)) {
                    require_once($fileToLoadSql);
                }
            }
        }
    }

    $__LOADER = new AutoLoader();
?>