I have a fairly simple folder structure where the main classes are in a folder, and any classes used by those classes are in a subfolder with the same name as that class.


I am trying to come up with a simple autoloader for this naming convention.

So far, my idea is to use underscores in the class name wherever there might be a forward slash in the class path. This way I can simply replace all underscores with forward slashes in the autoloader, and load the class file.

This means that the class name found in file:


would be:


These class names end up being very long however, and I would like to shorten them while still having a simple autoloader for this folder structure.

Any ideas or thoughts on how to achieve both short class names and a simple autoloader for this folder structure?

  • 1
    And what do you do if one of those classes is used by more than it's parent class? ;) – Martin Blore Apr 13 '11 at 18:56
  • @Martin Blore Then it becomes a "main" class in the root folder, and I pass it via dependency injection most likely. – dqhendricks Apr 13 '11 at 19:09
  • @Martin Blore These subfoldered classes are mainly drivers and stuff. Not much of a chance they would be used by multiple classes. – dqhendricks Apr 13 '11 at 19:14
  • 1
    My goal however is that these main classes should not need each other. I should basically be able to drop them in anywhere to any project. The only dependency would be this autoloader. There are a few cases where data access needs to be passed in however. – dqhendricks Apr 13 '11 at 19:19

How should I design a PHP class autoloader?

You shouldn't.

Leads from a number of PHP frameworks (Zend Framework, CakePHP, etc) and applications (Joomla, phpBB, etc) came together in late 2009 to build the PSR-0 Autoloader Proposal, which dictates a class file naming scheme and namespace mechanism.

It's remarkably similar to your existing system, with the simple inclusion of PHP 5.3 namespaces.

It's simple, it's straightforward, and if all goes well, it'll Just Work(tm) with a large body of third party code in the future.

  • Love this. Getting my server to upgrade to 5.3 now. – dqhendricks Apr 14 '11 at 17:38
  • For the record, I disagree. PSR-0 is a mess but it's also PSR-4. Both are "standard" but the pro to use is really limited and not work with the code of the future. – magallanes Jun 25 '18 at 12:29

If your version of PHP supports it, use namespaces.

namespace Session;

class Driver {

     // etc...


In the auto-loader...

if (file_exists($path = str_replace('\\', '/', strtolower($class)))
     require $path;
  • I'm not sure I understand how this helps with the above question? – dqhendricks Apr 14 '11 at 1:59
  • Let the namespaces follow your folder structure. So if you have "session" folder, make everything within that folder in the "session" namespace. It will give you a clean method of knowing how to locate the class file in your auto-loader. – TaylorOtwell Apr 14 '11 at 3:37
  • I think I see. Unfortunately my server is shared and running 5.2 something. No namespaces. – dqhendricks Apr 14 '11 at 3:40
  • Switch server :-) – Htbaa Apr 14 '11 at 8:00

Since no one mentioned it but now a days composer is the key thing to do auto-loading in PHP.

  1. Its a widely use small system to autoload the classes found in any structure. With the aid of composer.json you can effectively customize your directory structure and just autoload the autloader file created
  2. You can then download known github lib files that will easily plug into your projects
  3. As it is best practice - don't reinvent the wheel.

tutorials (as the man of composer might be a bit slim) can be found in such places like http://code.tutsplus.com


If you can use namepaces you can make use of those. Your autoloader should be able to register them and uses a 'prefix' to find them. So you can make this possible:

The class Reader in the namespace Application\Core\Database\Table could have a prefix of DBTable. So the class name is DBTable_Reader, which lives in the namespace Application\Core\Database\Table.

This prevents the long names, but looses some of the clearity. If you take a look at the autoloader of Symfony or Zend Framwork you'll get a lot of inspiration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.