PSR-0 自动加载类,简单框架

psr-0规范:

1.命名空间和类必须有以下结构 \<Vendor Name>\(<Namespace>\)*<Class Name>
2.每个命名空间必须有顶级的命名空间(“提供者”)
3.每个命名空间可以有任意多个子命名空间
4.从文件系统加载时,每个名称空间分隔符都会转换为DIRECTORY_SEPARATOR(操作系统路径分隔符)。
5.类别名称中的每个_字符都将转换为DIRECTORY_SEPARATOR。 _字符在名称空间中没有特殊含义。
6.从文件系统加载时,标准的名称空间和类的后缀为.php。
7.供应商名称,名称空间和类名称中的字母字符可以是小写和大写的任意组合。

开始创建

- app
- Autoload
- index.php
  1. 创建自动加载类
<?php

namespace Autoload;

/**
 * 
 */
class Loader
{
    
    static function autoload($class)
    {
        require BASEDIR.'/'.str_replace('\\','/',$class).'.php';

    }
}

2.index文件为入口文件包含自动加载类

define('BASEDIR',__DIR__);


include(BASEDIR.'\Autoload\Loader.php');

spl_autoload_register('\\Autoload\\Loader::autoload');

spl_autoload_register

spl_autoload_register — 注册给定的函数作为 __autoload 的实现

[手册](https://www.php.net/manual/zh/function.spl-autoload-register.php)

猜你喜欢

转载自www.cnblogs.com/rohanCh/p/11706901.html