douba7784 2011-03-28 04:40
浏览 101
已采纳

令人困惑的“无法重新声明类”错误(PHP)

I have a strange issue. I have a static method that loads classes (load_library). When it loads a particular class, it gives me a "cannot redeclare class" fatal error, but when testing whether the class exists right before using the load_library method to load it, it says the class does not exist. The load_library method works elsewhere without such errors.

If I take the load_library call out, it says it cannot find the class when the class is actually used a few lines later. Stranger still, if I take out my registered class autoload function instead, everything works perfectly, even though this autoload function doesn't even check the directory that the class I'm trying to load is in.

It's a complicated problem involving many files so it is hard to post code, but does this problem smell familiar to anyone out there?

My load_library method:

    public static function load_library($name) {
        if (!class_exists($name)) {
            if (file_exists('application/libraries/' . $name . '.php')) {
                include('application/libraries/' . $name . '.php');
            } else {
                trigger_error('Request made for non-existant library ('.$name.').', E_USER_ERROR);
            }
        }
    }

My call to the load_library method:

lev::load_library('lev_unit_tester/lev_base_test');

My registered autoload method:

    public static function autoloader($name) {
        if (class_exists($name)) return;
        if (file_exists('application/libraries/' . $name . '.php')) {
            include('application/libraries/' . $name . '.php');
        }
    }

The class I am trying to load (this is where the error occurs):

abstract class lev_base_test {

}

The actual error message:

Fatal error: Cannot redeclare class lev_base_test in /some/path/application/libraries/lev_unit_tester/lev_base_test.php on line 5

  • 写回答

2条回答 默认 最新

  • doumao8803 2011-03-28 04:44
    关注

    try this

    // Check to see whether the include declared the class
        if (!class_exists($class, false)){..}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作