2

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

mauris
  • 42,982
  • 15
  • 99
  • 131
dqhendricks
  • 19,030
  • 11
  • 50
  • 83

2 Answers2

0

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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • strange : it seems that `class_exists()` receives the same `$name` parameter as the `load_library` method -- and the fatal error message you get shows that `$name` is 'lev_unit_tester/lev_base_test' – Pascal MARTIN Mar 28 '11 at 04:45
  • the class_exists call uses a literal string as an argument, where th load_library call uses a path/filename as an argument... – dqhendricks Mar 28 '11 at 04:48
  • my class exists call looked like this: if (!class_exists('lev_base_test')) echo 'blahblahblah'; – dqhendricks Mar 28 '11 at 04:50
  • im not sure you read the question very thouroughly. I know how the autoload works. the autoload cannot possibly even load this class file, because it does not check that directory for class files at all. the only reason i mention it, is because it's removal seems to stop the error from happening, which does not make sense at all. – dqhendricks Mar 28 '11 at 04:55
  • It's a bit early, so I might be wrong, but I don't see that hard-coded `class_exists()` call in the code you posted ; instead, I see `class_exists($name)`, with `$name` being passed to `lev::load_library` as `'lev_unit_tester/lev_base_test'` – Pascal MARTIN Mar 28 '11 at 04:59
  • ah, i see your point, and that is a potential bug, but it is not what is causing my problem, since i only call the load_library on this class once. – dqhendricks Mar 28 '11 at 05:02
  • the hardcoded one i was testing with occured before the load_library call. – dqhendricks Mar 28 '11 at 05:03
  • i understand now... class_exists calls an autoload function automatically... I dislike that. – dqhendricks Mar 28 '11 at 05:07
0

try this

// Check to see whether the include declared the class
    if (!class_exists($class, false)){..}
Gaurav
  • 28,447
  • 8
  • 50
  • 80