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 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并