doubi7306
2014-07-25 16:01
浏览 46
已采纳

spl_autoload_register加载类两次

I keep trying to know what the problem is with this very simple class loader script. The class loader looks like this:

#src/vendors/Autoloading/lib/ClassLoader.php

namespace App\Vendors\Autoloading;

class ClassLoader
{
    private $path; 

    function __construct($path)
    {
        $this->path         = $path; 
    }

    public function load($class)
    {
      if(file_exists(  $class = str_replace(array('\\', '_'), DIRECTORY_SEPARATOR, $this->path) . '.php')){
            require $class;
            return true;
        }
    }

    public function register()
    {
        return spl_autoload_register([$this, 'load']);
    }
}

The initial class loader had more methods and some functions to validate the file names ... but, in the process of debugging I had to narrow it to that.

So, that class loader is being required inside an autoload.php file, as you can see below.

#src/vendors/autoload.php

namespace App\Vendors;

require 'Autoloading/lib/ClassLoader.php';
$autoload = new Autoloading\ClassLoader('path/Foo/FooClass');
$autoload->register(); 

The FooClass.php is located in src/Foo/FooClass.php

namespace App\Foo;
class FooClass{}

and there is actually no problem with the autoloading part, the class gets loaded just fine, but it is done twice which shows me the below error. I am calling it from an index.php file

<?php 

use \App\Foo\FooClass;
FooClass::somefunction(); 

Just using that generates this error.

Fatal error: Cannot redeclare class path\foo\FooClass in /path/to/index.php on line 4

图片转代码服务由CSDN问答提供 功能建议

我一直试图通过这个非常简单的类加载器脚本来了解问题所在。 类加载器如下所示:

  #src / vendors / Autoloading / lib / ClassLoader.php 
 
namespace App \ Vendors \ Autoloading; 
 
class ClassLoader \  n {
 private $ path;  
 
函数__construct($ path)
 {
 $ this-&gt; path = $ path;  
 
 
 
公共函数加载($ class)
 {
 if if(file_exists($ class = str_replace(array('\\','_'),DIRECTORY_SEPARATOR,$ this-&gt; path)。  '.php')){
需要$ class; 
返回true; 
} 
} 
 
公共函数寄存器()
 {
返回spl_autoload_register([$ this,'load'])  ; 
} 
} 
   
 
 

初始类加载器有更多方法和一些函数来验证文件名... 但是,在调试过程中 我不得不缩小它。

因此,在 autoload.php 文件中需要该类加载器,如下所示。

   #src / vendors / autoload.php 
 
namespace App \ Vendors; 
 
require'Autoloading / lib / ClassLoader.php'; 
 $ autoload = new Autoloading \ ClassLoader('path / Foo / FooClass'  ); 
 $的autoload-&GT;寄存器();  
   
 
 

FooClass.php 位于 src / Foo / FooClass.php \ n

 命名空间App \ Foo; 
class FooClass {} 
   
 
 

自动加载部分实际上没有问题,类得到 加载得很好,但它完成了两次,这显示了以下错误。 我从 index.php 文件中调用它

 &lt;?php 
 
use \ App \ Foo \ FooClass; 
 
FooClass ::  somefunction();  
   
 
 

只是使用它会产生此错误。

致命错误:无法在第4行的/path/to/index.php中重新声明类路径\ foo \ FooClass \ n

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongzhangnong2063 2014-07-25 16:20
    已采纳

    Your autoloading function is wrong:

    public function load($class)
    {
        if (
            file_exists(  
                $class = str_replace(
                    array('\\', '_'), 
                    DIRECTORY_SEPARATOR, 
                    $this->path)
                . '.php')
        ) {
            require $class;
            return true;
        }
    }
    

    The function is calles with the name of the class to be loaded (if impossible, the function should do nothing).

    What you do is ignoring the class name, and create a new one based on the path the autoloader is created with. This will always load the same file, with the same class, even if there are different classes to be loaded.

    And this explains why you get the error, because no matter which class name gets passed, you always include the one file that is related to the path.

    You probably want to use a proper PSR-0 or PSR-4 autoloader. I would recommend using the one that comes with Composer, as you are likely to be using Composer sooner or later yourself. Why not starting today?

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drlh197610 2014-07-25 16:07

    Try using require_once instead of require. Your autoloader won't keep track of what files have been loaded so far.

    Here's how I set mine up. I don't use a class. Maybe this will help

    function PlatformAutoloader($classname) {
        try {
            // Change \ to / so namespacing will work
            $classname = strtolower(str_replace('\\', DIRECTORY_SEPARATOR, $classname));
    
            if(!@include_once(DIR_CLASSES . DIRECTORY_SEPARATOR . $classname . '.php')) return false;
        } catch(Exception $e) {
            return false;
        }
    }
    spl_autoload_register('PlatformAutoloader');
    
    评论
    解决 无用
    打赏 举报
  • doushi9444 2014-07-25 16:08

    Check if the class already exists using class_exists before checking for the file

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题