spl_autoload_register装在函数的正确写法

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();

 

posted @ 2015-07-30 15:28  北京流浪儿  阅读(449)  评论(0编辑  收藏  举报