<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Autoloader
 * 自动加载器
 * @author jgao
 */
class Autoloader {

    private $directorys;

    public function __construct() {
        $this->directorys = array(
            __DIR__ . "/utils/",
            __DIR__ . "/models/",
            __DIR__ . "/providers/",
        );
    }

    /**
     * Registers the autoloader class with the PHP SPL autoloader.
     *
     * @param bool $prepend Prepend the autoloader on the stack instead of appending it.
     */
    public static function register($prepend = false) {
        spl_autoload_register(array(new self(), 'autoload'), true, $prepend);
    }

    /**
     * Loads a class from a file using its fully qualified name.
     *
     * @param string $className Fully qualified name of a class.
     */
    public function autoload($className) {
        foreach ($this->directorys as $dir) {
            $filepath = $dir . $className . '.php';
            if (is_file($filepath)) {
                require $filepath;
                break;
            }
        }
    }

}
