Search code examples
phpzend-frameworkmodel-view-controllerautoloadzend-autoloader

Auto load classes by class name like Zend Framework


How can I auto load my framework controllers and models by their class name like the Zend Framework does?

Zend Framework auto loads classes like so:

new Application_Controller_Index();

meaning that controller class is located at application/controllers/IndexController.php


Solution

  • Why not to have a look in the source code ? http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Loader.php

    just in case, probably the simplest way:

    spl_autoload_register(function($classname){
        include str_replace('_', DIRECTORY_SEPARATOR, $classname) . '.php';
    });