Blogger Information
Blog 143
fans 1
comment 0
visits 440504
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
关于php命名空间与自动加载
弘德誉曦的博客
Original
860 people have browsed it
namespace EFrame;

class Autoloader
{
    /医院
     * Load files by namespace.
     *
     * @param string $name
     * @return boolean
     */
    public static function loadByNamespace($name)
    {
        $class_path = str_replace('\\', DIRECTORY_SEPARATOR, $name);
        if (strpos($name, 'EFrame\\') === 0) {
            $class_file = __DIR__ . substr($class_path, strlen('EFrame')) . '.php';
        }else {
            $class_file = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Applications'.DIRECTORY_SEPARATOR.$class_path.'.PHP';
        }

        if (is_file($class_file)) {
            require_once($class_file);
            if (class_exists($name, false)) {
                return true;
            }
        }
        return false;
    }
}

spl_autoload_register('\EFrame\Autoloader::loadByNamespace');

在加载类中的核心问题是定义需要自动加载的类文件路径$class_path

实现自动加载的核心在于注册自动加载类(该声明了自动加载规则)spl_autoload_register('\EFrame\Autoloader::loadByNamespace')

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post