Question

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

Was it helpful?

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';
});

OTHER TIPS

Just use function __autoload and you should be all set.

There are really good examples on the PHP manual: http://php.net/manual/en/language.oop5.autoload.php

Also please note that the __autoload might be deprecated soon. To go around that, use the spl_autoload_register() as the manual says.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top