1
votes

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

2 Answers

0
votes

Your are calling your load_library method this way :

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.

So class_exists() returns false ; and, so, you try to include the file that contains your class every time the load_library method is called.


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.

0
votes

try this

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