duanji1056 2014-07-13 08:09
浏览 63
已采纳

当命名空间使用时,PHP spl_autoload_register无法加载具有require范围的类

Ok!!! Now, i feel like clue less why spl_autoload_register() can't able to load the class.

My folder structure is like this..

  • application

    • controller
      • welcome.php
  • system

    • core
      • BaseController.php
      • Load.php
  • index.php

My BaseController.php code

<?php
namespace system\core;

class BaseController {
    public function __construct() {
        spl_autoload_register(array($this, 'loader'));
    }

    private function loader($className) {              
        $className = ltrim($className, '\\');
        $fileName  = '';
        $namespace = '';
        if ($lastNsPos = strrpos($className, '\\')) {
            $namespace = substr($className, 0, $lastNsPos);
            $className = substr($className, $lastNsPos + 1);
            $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
        }
        $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

        require $fileName;
    }
}

My Load.php code

<?php
namespace system\core;

class Load  {
    public function view()
    {
        echo "Method for loading view";
    }
}

My welcome.php code

<?php
class Welcome extends system\core\BaseController {
    public function index()
    {
        $obj_load = new Load();
        $obj_load->view();
    }
}

My index.php code

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once "system/core/BaseController.php";
require_once "application/controller/welcome.php";
$welcome = new welcome();
echo $welcome->index();

When i'm executing this code(index.php) I'm getting following error...

Fatal error: Class 'Load' not found in /var/www/nut/test/application/controller/welcome.php on line 5

But, If i remove the namespace from Load.php i'm not getting any error. I can't able to understand why that namespace(used in Load.php) is creating error.

Any thought...

Regards

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-07-13 10:22
    关注

    As Mark has noted, you are using the wrong character when trying to include your files. This page should make things clearer.

    Edit: You had quite a few mistakes in your code. The whole point of namespaces is that you have to reference them properly (as well as declare them properly, you failed to do both, that's why the autoload code didn't work) Also the autoload code had no real chance to run since you had it hidden inside a constructor to a class that never gets loaded in the first place. Try this:

    index.php

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    
    require_once("system/core/AutoLoader.php");
    $welcome = new \application\controller\welcome();
    echo $welcome->index();
    

    welcome.php (note the lower case class name and the addition of the namespace)

    <?php
    
    namespace application\controller;
    
    class welcome extends \system\core\BaseController {
        public function index()
        {
            $obj_load = new \system\core\Load();
            return $obj_load->view();
        }
    }
    

    Load.php

    <?php
    namespace system\core;
    
    class Load  {
        public function view()
        {
            return "Method for loading view";
        }
    }
    

    BaseController.php

    <?php
    namespace system\core;
    
    class BaseController {
    //  public function __construct() {
    //      spl_autoload_register(array($this, 'loader'));
    //  }
    //
    //  private function loader($className) {
    //      $className = ltrim($className, '\\');
    //      $fileName  = '';
    //      $namespace = '';
    //      if ($lastNsPos = strrpos($className, '\\')) {
    //          $namespace = substr($className, 0, $lastNsPos);
    //          $className = substr($className, $lastNsPos + 1);
    //          $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    //      }
    //      $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
    //
    //      require $fileName;
    //  }
    }
    

    new file: system\core\AutoLoader.php

    <?php
    
    namespace system\core;
    
    class AutoLoader {
    
        static public function loader($className) {
            $className = ltrim($className, '\\');
            $fileName  = '';
            $namespace = '';
            if ($lastNsPos = strrpos($className, '\\')) {
                $namespace = substr($className, 0, $lastNsPos);
                $className = substr($className, $lastNsPos + 1);
                $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
            }
            $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
    
            require $fileName;
        }
    
    }
    
    spl_autoload_register(array('\system\core\AutoLoader', 'loader'));
    

    I also changed the code to return the string instead of displaying it since you are already echoing it inside index.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算