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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀