spl_autoload_register装在函数的正确写法

时间:2023-03-08 22:19:06
AutoLoading\loading

<?php
namespace AutoLoading; class Loadind {
public static function autoload($className){
//根据PSR-O的第4点 把 \ 转换层(目录风格符) DIRECTORY_SEPARATOR ,
//便于兼容Linux文件找。Windows 下(/ 和 \)是通用的
//由于namspace 很规格,所以直接很快就能找到
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, DIR . '\\'. $className) . '.php';
if (is_file($fileName)) {
require $fileName;
} else {
echo $fileName . ' is not exist'; die;
} }
}

index.php

//定义当前的目录绝对路径
define('DIR', dirname(__FILE__));
//加载这个文件
require DIR . '/loading.php';
//采用`命名空间`的方式注册。php 5.3 加入的
//也必须是得是static静态方法调用,然后就像加载namespace的方式调用,注意:不能使用use
spl_autoload_register("\\AutoLoading\\loading::autoload");
// 调用三个namespace类
//定位到Lib目录下的Name.php
Lib\Name::test();
//定位到App目录下Android目录下的Name.php
App\Android\Name::test();
//定位到App目录下Ios目录下的Name.php
App\Ios\Name::test();