Search code examples
phpclasspackageconventionsautoload

Does this directory structure to organize classes like Zend Framework have a name?


Ok in very simple, small projects it is probably common to have classes defined in files that are named accordingly. So that for example this autoload function would work:

function __autoload($class_name) {
    require_once $class_name . '.php';
}

But when a project gets bigger this can be a real mess and names could be misleading and confusing. To solve this issue I have seen a lot of approaches.

Some companies prefix all their classes... like "class.MyClass.php" to clean up some mess on one level.

Some organize them in directories. But they all have to be added to include path with slows down an application or have to be changed into before usage. Code can become complicated and strange.

PHP introduced namespaces since version 5.3 altough i am not really sure about their functionalities yet.

However a standard that I have seen more and more often which I think got popular because of the zend framework is the following:

function __autoload($className){
    require_once  str_replace('_','/',$className).'.php';
}

This trivial autoloader already expresses the concept.

Class names are preceded by their path with underline as a delimiter.

For example the "Service" component holds apis for various service providers.

Zend/Service/Amazon.php holds the class Zend_Service_Amazon.

A more specialised API for Amaozn s3 can be found in Zend/Service/Amazon/S3.php where the class Zend_Service_Amazon_S3 lives.

I think this concept is very easy to maintain, very intuitive, quite common by now and is adaptable to most needs.

But how is it called. Does it have a name?


Solution

  • Finally found the right answer. Its called PSR-0 and is a compliance guideline for autoloading:

    http://groups.google.com/group/php-standards/web/psr-0-final-proposal