You have to check class_exists
twice.
- To autoload if necessary
- To see if class now exists after autoload
My working code is:
$autoload = spl_autoload_functions() ? true : false; // spl_autoload_functions can return array, empty array or false, but we need boolean
$should_include = $autoload ? class_exists($className, true) : true;
if($should_include && !class_exists($className, false)){ // make sure the class does not exist before including
if(is_readable($fileName)){
include_once($fileName);
} else {
throw new Exception('Could not include ' . $className);
}
}
This attempts to use previously-defined autoloaders before falling back.