About zendframework class auto-load directory structure zf-demo & nbsp; application & nbsp; controller & nbsp; & nbsp; IndexControl automatic loading of zendframework classes
Directory structure
zf-demo
application
controller
IndexController.php
models
User.php
services
views
Bootstrap.php
The class name of user. php is Model_User. in IndexController. php, $ user = new Model_User () is called ();
An error is reported during execution.
Fatal error: Class 'model _ user' not found in D: \ lamp \ sites \ zf-demo \ application \ controllers \ IndexController. php on line 132
Search online to get the answer
Add the _ initAutoload method to Bootstarp. php.
Bootstrap. php code
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initAutoload() {
$moduleLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH));
/** auto load */
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
return $moduleLoader;
}
}
Run the command again to load the user class.
Problem:
In my understanding, zend's automatic loading mechanism replaces the underline in the class name with the path separator, and then loads the class according to the sequence of include_path.
In this case, Model_User will be converted to Model/User. php for loading, but the directory does not contain the model folder. only models is available. Why can it be loaded successfully ?, _ What does initAutoload do? Who can explain it?
------ Solution --------------------
Reference:
Reference:
You can view the code.
Application_Model_User should be normal. Your path is incorrect.
Automatic loading. one is based on the above automatic conversion path. Second, search for all related classes based on the directory.
The automatic loading in controller and the automatic loading in bootstrap may use the same class, but you have specified the path.
You mean the class name of user. php should be Applic ......
Yes! This is a default type! At least the example of quick start is like this. The command line create model user is automatically created. The models folder.
------ Solution --------------------
Generally, this irregular namespace uses Zend_Loader_Autoloader_Resource. For convenience, the structure inheritance of the Framework application implements Zend_Application_Module_Autoloader, which simplifies the operation and knows the source code.
Source code of the following classes
/**
* Resource loader for application module classes
*
* @ Uses Zend_Loader_Autoloader_Resource
* @ Category Zend
* @ Package Zend_Application
* @ Subpackage Module
* @ Copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @ License http://framework.zend.com/license/new-bsd New BSD License
*/
Class Zend_Application_Module_Autoloader extends Zend_Loader_Autoloader_Resource
{
/**
* Constructor
*
* @ Param array
------ Solution --------------------
Zend_Config $ options
* @ Return void
*/
Public function _ construct ($ options)