Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using multiple autoloaders php

Tags:

php

autoloader

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

like image 722
Reshad Avatar asked Nov 12 '12 14:11

Reshad


1 Answers

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

like image 115
Explosion Pills Avatar answered Oct 19 '22 00:10

Explosion Pills