简体   繁体   English

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

[英]confusing “cannot redeclare class” error (PHP)

I have a strange issue. 我有一个奇怪的问题。 I have a static method that loads classes (load_library). 我有一个加载类的静态方法(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. 当它加载一个特定的类时,它给我一个“无法重新声明类”的致命错误,但是当在使用load_library方法加载该类之前测试该类是否存在时,它表示该类不存在。 The load_library method works elsewhere without such errors. load_library方法可在其他地方使用而不会出现此类错误。

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. 如果我将load_library调出,它说当稍后实际使用该类时,它找不到该类。 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. 仍然很陌生,如果我改用我注册的类的autoload功能,那么即使该autoload功能甚至不检查我要加载的类所在的目录,也可以正常运行。

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: 我的load_library方法:

    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: 我对load_library方法的调用:

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 致命错误:无法在第5行的/some/path/application/libraries/lev_unit_tester/lev_base_test.php中重新声明lev_base_test类

Your are calling your load_library method this way : 您正在以这种方式调用load_library方法:

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

Which means you are testing, with class_exists() , if there is a class called lev_unit_tester/lev_base_test -- which is probably not quite the case : that's not a valid name for a class. 这意味着您正在使用class_exists()进行测试,如果有一个名为lev_unit_tester/lev_base_test的类-可能并非如此:这不是一个有效的类名。

So class_exists() returns false ; 所以class_exists()返回false ; and, so, you try to include the file that contains your class every time the load_library method is called. 因此,您尝试在每次调用load_library方法时包括包含您的类的文件。


There is no problem when you are using autoloading, because PHP will only call your autoloader when it you try to use a class which has not been defined : once the file that correspond to a class has been included, the autoloaded will not be called for that class again. 使用自动加载时没有问题,因为PHP仅在您尝试使用未定义的类时才会调用自动加载器:一旦包含了与该类对应的文件,就不会调用自动加载器再次上课。

try this 尝试这个

// Check to see whether the include declared the class
    if (!class_exists($class, false)){..}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM