Search code examples
phpautoloadglobspl-autoload-register

Lazy Classes Includes PHP


I use a autoloader to include classes. What i'm doing right now is using "glob" to read different dir's and push them in an array. Is there a better solution to this?

$path = './';

$files = array_merge(
glob($path.'includes/classes/system/*.class.php'),
glob($path.'includes/classes/system/baseclasses/*.class.php'),
glob($path.'includes/classes/system/systementities/*.class.php'));

EDIT:

I use this inside an autoload class. The problem is that I have to search for the files with glob. Is there a faster way to do this?


Solution

  • Autoloading will be triggered whenever you are trying to use a class that isn't know to PHP. If you use include/require, PHP will go through all the directories you specified for your include_path in PHP.ini, so there should no reason to use glob. In your case, it should be sufficient to set the include_path to

    /path/to/includes/classes/system/
    

    The fastest way to include files would be to use a class2file map. However, this requires you to create such a map and keep it updated when you modify your application.