1

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

Ty Bailey
  • 2,392
  • 11
  • 46
  • 79

2 Answers2

3

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';
});
b.b3rn4rd
  • 8,494
  • 2
  • 45
  • 57
0

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.

Lasse
  • 431
  • 5
  • 17
  • I understand I need to use an auto load function, I'm asking how Zend has it set up so they don't need to specify a directory, just include the class name which is then parsed into a directory to find the class. – Ty Bailey Apr 23 '13 at 00:54
  • To use the format you specified above, you have to [explode()](http://www.php.net/manual/en/function.explode.php) the class name in to bits and get the path from the array. – Lasse Apr 23 '13 at 00:57