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

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

  • 写回答

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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制