Zend框架:自动加载类库

时间:2010-01-28 01:51:16

标签: php zend-framework autoload zend-autoloader

我在这里定义了一个类库... / 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_Myclass”类

有什么想法吗?

潜在的相关杂记:

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

有人建议更改我尝试的“默认”,但它似乎没有解决问题,并且使用此命名空间破坏了模型功能的负面影响。

4 个答案:

答案 0 :(得分:13)

你的班级必须是名字Me_Myclass:

class Me_Myclass
{
}

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

/
    /application
    /library
    /public

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

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

答案 1 :(得分:2)

您可以在config.ini文件中定义自动加载目录,如下所示:

autoloaderNamespaces[] = "Me_"


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

100%

答案 2 :(得分:1)

我认为@ smack0007意味着用Zend_Loader_Autoloader :: getInstance() - &gt; registerNamespace('Me_')替换_initAutoload方法的内容;所以它看起来像这样:

protected function _initAutoload()
{
    Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_');
}

答案 3 :(得分:0)

不确定这是不是你的问题,但我只是花了最后一天半试图弄清楚我自己的类似问题(第一次在Windows上从Linux上加载)。事实证明我对我的图书馆文件夹名称视而不见。

/library
    /Tlib

与(on * nix)

不同
/library
    /tlib

类名通常是这个

class Tlib_FooMe {
 ...
}

希望这可以帮助那些同样心不在焉的人。