Aaron Holbrook
I'm Aaron Holbrook: a web engineer, father, gamer and geek.
Screen Shot 2015-06-04 at 3.00.51 PM

Simpler loading with Autoload!

Purpose

Automatically load all PHP files in the specified directory. Recursively.

Benefits

  • Rename files with no fear of breaking includes or require calls
  • Encourages using more files to more cleanly organize code into smaller logical chunks
  • Reduce git merge conflicts with other developers

Usage

Install via composer by adding the package to your composer file. "aaronholbrook/autoload": "1.*", does the trick.

Be sure to include the regular composer load file via require( __DIR__ . '/vendor/autoload.php' );.

Please note that although similarly named, this Autoload library is meant for loading all PHP files in a given directory. This does NOT function in the manner of the built-in PSR-4 PHP Autoloader.

Simply load your desired includes or whatever directory by calling:

\AaronHolbrook\Autoload\autoload( __DIR__ . 'includes' );

Caveats

Since this is a recursive loader, you should be conscious of what you’re placing in your autoloaded directory.

Things I wouldn’t recommend doing:
* Placing a big (or any) PHP library in the autoloaded directory (this should/could be handled better with composer anyways!)
* Being lax with permissions on a server. Obviously this is never a good idea, but I would be sure that your folder / file permissions are up to snuff (or strange files may be loaded)

Video Walkthrough

Disclaimer

Be aware that this may not be the right choice for your project. Please be fully aware of what this plugin does and how it works.

Links

Github
Packagist



  • Andy Stratton

    Whoops, previous comment blocked my code! If you’re simply doing a single directory, or even 2 directories:

    https://gist.github.com/theandystratton/f3aeff6e2378f7edf801

    • Aaron Holbrook

      Yea – I wanted a nice, reusable library that I could simply include via composer which allowed me to rearrange my files on a whim. Ends up being very beneficial to keep your code from stagnating and becoming inflexible.

      • Andy Stratton

        Oh yeah! I have been doing some incarnation of this for a long time, not only for changing file names but adding tons of organized files without having to deal with adding require/include/etc.