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条回答 默认 最新

  • duangou1933 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

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题