Using the ZF autoloader to load models
I have a simple ZF application (no modules) which works fine when I require
classes before I use them. However, I'd like to use the ZF autoloader to load them automatically (which I assumed was default behaviour).
How would I go about doing this? I'm a bit confused by the new(ish) Zend_Application
way of doing things. My directory structure is the standard one:
application/
controllers/
models/
views/
scripts/
Bootstrap.php
For example removing the require in this method:
class HomeController extends Zend_Controller_Action {
public function indexAction() {
$tasks = arra开发者_Go百科y();
//require 'models/Task.php';
$tasks[] = new Application_Model_Task(array(...));
}
}
If you use Zend Frame Version 1.8 (+) put this in your Bootstrap.php:
/**
* Initialize the autoloader
*
* @return Zend_Application_Module_Autoloader
*/
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'My',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
And now try :
$model = new My_Model_Task();
精彩评论