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

标签 php namespaces spl-autoload-register

行!!!现在,我不知道为什么 spl_autoload_register() 无法加载该类。

我的文件夹结构是这样的..

  • 应用
  • Controller
  • 欢迎.php
  • 系统

  • BaseController.php
  • 加载.php
  • index.php

  • 我的 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;
        }
    }
    

    我的 Load.php 代码
    <?php
    namespace system\core;
    
    class Load  {
        public function view()
        {
            echo "Method for loading view";
        }
    }
    

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

    我的 index.php 代码
    <?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();
    

    当我执行此代码(index.php)时,出现以下错误...
    Fatal error: Class 'Load' not found in /var/www/nut/test/application/controller/welcome.php on line 5
    

    但是,如果我从 Load.php 中删除命名空间,我不会收到任何错误。我无法理解为什么该命名空间(在 Load.php 中使用)正在创建错误。

    任何想法...

    问候

    最佳答案

    正如 Mark 所指出的,您在尝试包含文件时使用了错误的字符。 This页面应该使事情更清楚。

    编辑:
    您的代码中有不少错误。命名空间的全部意义在于你必须正确引用它们(以及正确声明它们,你没有做到这两者,这就是自动加载代码不起作用的原因)而且自动加载代码没有真正的机会运行,因为你有它隐藏在一个从一开始就从未加载的类的构造函数中。试试这个:

    索引.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(注意小写的类名和命名空间的添加)
    <?php
    
    namespace application\controller;
    
    class welcome extends \system\core\BaseController {
        public function index()
        {
            $obj_load = new \system\core\Load();
            return $obj_load->view();
        }
    }
    

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

    基础 Controller .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;
    //  }
    }
    

    新文件: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'));
    

    我还更改了代码以返回字符串而不是显示它,因为您已经在 index.php 中回显了它

    关于使用命名空间时,PHP spl_autoload_register 无法加载具有 require 范围的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24720724/

    相关文章:

    c++ - 如何让普通的 C++ 编译器打印某行所在的命名空间?

    php - 自定义自动加载与 Composer 的自动加载冲突?

    php - 将 Composer 的自动加载器与个人代码一起使用

    php - 我可以在 PHP 中混合使用 MySQL API 吗?

    php - 删除类别之前更新相关帖子

    php - 使用命名空间找不到类

    .net - 我可以从构建配置中排除命名空间(即 Name1.Name2)吗

    php - php spl_autoload_register 和 composer autoloader 可以一起工作吗?

    php - 一个 SQL 语句来处理相同值的 NULL 和 STRING 可能性

    php - json_encode 来自数据库的数据数组