Automatic loading of zendframework

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.