using multiple autoloaders php

Reshad picture Reshad · Nov 12, 2012 · Viewed 14.6k times · Source

Hello I am trying to use SILEX micro framework together with my own library full of classes and therefore I am stuck with 2 loaders that results in a error that the loader can't load classes.. Is there a way to use these 2 loaders simultaneously without getting this error?

the loader that I use you can find below:

    <?php

/*
 * Loader
 */

function my_autoloader($className) 
{
// haal de base dir op.
  $base = dirname(__FILE__);


  // het pad ophalen
  $path = $className;

  // alle paden samenvoegen tot waar ik zijn moet en de phpfile eraan plakken.
  $file = $base . "/lib/" . $path . '.php';       

  // als file bestaat haal op anders error
  if (file_exists($file)) 
  {
      require $file;
  }
  else 
  {
      error_log('Class "' . $className . '" could not be autoloaded');
      throw new Exception('Class "' . $className . '" could not be autoloaded from: ' . $file); 
  }
}

spl_autoload_register('my_autoloader');

?>

the loader that silex uses is in the vendor directory ( from the framework itself )

and this is how my file tree looks like:

filetree

Answer

Explosion Pills picture Explosion Pills · Nov 12, 2012

Don't throw errors in your autoloader functions. spl_autoload_register allows php to go through all registered autoloaders in order, but if you throw an uncaught error in the middle of that process it can't try the next autoloader.

http://php.net/spl_autoload_register