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条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法