Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

/exampleClass.php
/exampleClass/usedByExampleClass.php

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:

/exampleClass/usedByExampleClass.php

would be:

exampleClass_usedByExampleClass

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?

share|improve this question
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
add comment

4 Answers

up vote 5 down vote accepted

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.

share|improve this answer
    
Love this. Getting my server to upgrade to 5.3 now. –  dqhendricks Apr 14 '11 at 17:38
add comment

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;
}
share|improve this answer
    
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
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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