If you have a several classes in your PHP project then you already know how annoying it can be to constantly include the class files before instantiating your class. You may already know about spl_autoload and related functions, but it can still be a pain creating a function for each directory and registering each file within. An easier way to go about this is to recursively register every file in your root directory regardless of how deep your directory structure is. The easy re-usable solution is to create a function that will loop through all of your directories and include the file that matches the class.

   spl_autoload_register( 'autoload' );


  /**
   * autoload
   *
   * @param  string $class
   * @param  string $dir
   * @return bool
   */
  function autoload( $class, $dir = null ) {

    if ( is_null( $dir ) )
      $dir = '/path/to/project';

    foreach ( scandir( $dir ) as $file ) {

      // directory?
      if ( is_dir( $dir.$file ) && substr( $file, 0, 1 ) !== '.' )
        autoload( $class, $dir.$file.'/' );

      // php file?
      if ( substr( $file, 0, 2 ) !== '._' && preg_match( "/.php$/i" , $file ) ) {

        // filename matches class?
        if ( str_replace( '.php', '', $file ) == $class || str_replace( '.class.php', '', $file ) == $class ) {

            include $dir . $file;
        }
      }
    }
  }

Now you won’t need to include any class files to your project. I use this mostly for WordPress plugin development but it is a universal solution to the hassle of including class files or creating a function to loop each specific directory to include files.

8 comments on “Using spl_autoload_register to Load All Classes in a PHP Project

  1. Aleix says:

    Good job!!
    The best I see in the web!!

    I had some problems with, / because I am working in windows and I forgot to change to \.

    Good job and thanks for share it.

  2. Florian goussin says:

    This is great! I have been looking for that for a while. Thanks a lot!

  3. Florian goussin says:

    I don’t understand how the second parameter of the function autoload works.

    spl_autoload_register( ‘autoload’ ) will all the time call the function with $dir = null. Is my assumption correct ?

    1. Florian goussin says:

      ok I got it. The second argument is for the recursive call.

  4. Mike Johnson says:

    This looks great, I’ve been using something quite similar for a while now (except my function is not recursive in itself but rather uses PHP’s Recursive Directory Iterator on account of the fact that it’s implemented in straight C and should be considerably faster).

    However, as my project grows larger and larger, I’m starting to have performance related concerns… Did anyone happen to run some tests/benchmarks on this code? Also, it’d be cute if you could just debate a bit about the pros and cons of this technique vs. the infamous PSR-0, essentially why and when would you use one over the other?

  5. Salandru says:

    Spent a lot of time, even sleepless nights searching for a solution until this solution. Haven’t find anything as efficient as your contribution to development yet. Great job! Keep up the good work. Yuh a di real boss!

  6. Marcelo Barros says:

    Hi!

    Can you please, give me a example of implementation?

  7. Bob Pride says:

    Marvelous piece of machinery, thank you very much

Comments are closed.