代码之家  ›  专栏  ›  技术社区  ›  user250120

Zend框架:自动加载类库

  •  8
  • user250120  · 技术社区  · 15 年前

    我在这里定义了一个类库…/projectname/library/me/myclass.php,定义如下:

    <?php
    class Me_Myclass{
    }
    ?>
    

    我有以下引导程序:

    <?php
    
    /**
     * Application bootstrap
     * 
     * @uses    Zend_Application_Bootstrap_Bootstrap
     */
    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {
        /**
         * Bootstrap autoloader for application resources
         * 
         * @return Zend_Application_Module_Autoloader
         */
        protected function _initAutoload()
        {
            $autoloader = new Zend_Application_Module_Autoloader(array(
                'namespace' => 'Default',
                'basePath'  => dirname(__FILE__),
            ));
            $autoloader->registerNamespace('Me_');
            return $autoloader;
        }
    
        /**
         * Bootstrap the view doctype
         * 
         * @return void
         */
        protected function _initDoctype()
        {
            $this->bootstrap('view');
            $view = $this->getResource('view');
            $view->doctype('XHTML1_STRICT');
        }
    
        /**
         * Bootstrap registry and store configuration information
         * 
         * @return void
         */
        protected function _initRegistry()
        {
          $config = new Zend_Config_Ini(APPLICATION_PATH . 
                                          '/configs/application.ini', APPLICATION_ENV,
                                          array('allowModifications'=>true));
          Zend_Registry::set('configuration', $config);
        }
    
    }
    

    在我的控制器中,我尝试这样实例化类:

    <?php
    class SomeController extends Zend_Controller_Action
    {
        public function indexAction()
        {
            $classMaker=new Me_Myclass();
        }
    }
    ?>
    

    当我直接导航到http:/something.com/projectname/some时?id=1我得到以下错误:

    致命错误:在x行的/home/myuser/work/projectname/application/controllers/somecontroller.php中找不到类“me\u myclass”

    有什么想法吗?

    潜在的相关杂项:

    当我使用在应用程序/库下的其他文件夹中定义的类扩展模型时,自动加载程序似乎可以工作。

    有人建议更改“默认值”,我尝试了更改,但似乎没有解决问题,而且使用这个名称空间破坏模型的函数会产生额外的负面影响。

    4 回复  |  直到 13 年前
        1
  •  12
  •   smack0007    15 年前

    你的班级应该叫我“我的班级:

    class Me_Myclass
    {
    }
    

    将库文件夹向上移动一个级别,以便具有文件夹结构:

    /
        /application
        /library
        /public
    

    然后在引导程序中将以下内容添加到InitAutoLoad()中:

        Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_');
    
        2
  •  2
  •   Otto    13 年前

    您可以这样定义config.ini文件中的autoload dir:

    autoloaderNamespaces[] = "Me_"
    
    
    ;You could add as many as you want Classes dir:
    autoloaderNamespaces[] = "Another_"
    autoloaderNamespaces[] = "Third_"
    

    作品100%

        3
  •  1
  •   Wannabeweb    15 年前

    我认为@smack0007意味着用zend_loader_autoloader::getInstance()->registernamespace(“me”)替换您的initoload方法的内容,所以看起来是这样的:

    protected function _initAutoload()
    {
        Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_');
    }
    
        4
  •  0
  •   Edward Hew    15 年前

    不确定这是否是您的问题,但我只是花了最后一天半的时间来解决我自己的类似问题(第一次从Windows将其加载到Linux上)。结果发现我对图书馆的文件夹名案例视而不见。

    /library
        /Tlib
    

    与(on*nix)不同

    /library
        /tlib
    

    类名称通常为

    class Tlib_FooMe {
     ...
    }
    

    希望这能帮助同样心不在焉的人。

    推荐文章