Zend Framework:自动加载类库 [英] Zend Framework: Autoloading a Class Library

查看:96
本文介绍了Zend Framework:自动加载类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里定义了一个类库.../projectname/library/Me/Myclass.php定义如下:

I've got a class library in defined here .../projectname/library/Me/Myclass.php defined as follows:

<?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);
    }

}

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

In my controller I try to instantiate the class like this:

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

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

When I navigate directly to http:/something.com/projectname/some?id=1 I get the following error:

致命错误:在第x行的/home/myuser/work/projectname/application/controllers/SomeController.php中找不到类'Me_Myclass'

Fatal error: Class 'Me_Myclass' not found in /home/myuser/work/projectname/application/controllers/SomeController.php on line x

有什么想法吗?

可能相关的杂项:

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

The autoloader seems to work when I'm extending models with classes I've defined in other folders under application/library.

有人建议更改默认值",这是我尝试过的方法,但它似乎无法解决问题,并且具有破坏使用此命名空间的模型功能的负面影响.

Someone suggested changing the 'Default', which I attempted but it didn't appear to fix the problem and had the added negative impact of breaking function of models using this namespace.

推荐答案

您的班级需要命名为Me_Myclass:

You class needs to be name Me_Myclass:

class Me_Myclass
{
}

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

Move your library folder up a level so that you have the folder structure:

/
    /application
    /library
    /public

然后在Bootstrap中将以下内容添加到_initAutoload():

And then in your Bootstrap add the following to the _initAutoload():

    Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_');

这篇关于Zend Framework:自动加载类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆