Zend Framework:自动加载类库

发布于 2024-08-19 13:58:16 字数 2006 浏览 6 评论 0原文

我在这里定义了一个类库 .../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 我收到以下错误:

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

有什么想法吗?

可能相关的杂项:

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

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

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

<?php
class Me_Myclass{
}
?>

I've got the following bootstrap:

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

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

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

Any ideas?

Potentially Pertinent Miscellany:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

季末如歌 2024-08-26 13:58:16

您的类需要命名为 Me_Myclass:

class Me_Myclass
{
}

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

/
    /application
    /library
    /public

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

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

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

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

    Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_');
心意如水 2024-08-26 13:58:16

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

autoloaderNamespaces[] = "Me_"


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

works 100%

you can define the autoload dir in the config.ini file like this:

autoloaderNamespaces[] = "Me_"


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

works 100%

_失温 2024-08-26 13:58:16

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

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

I think @smack0007 means replace the contents of your _initAutoload method with Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_'); so it looks like this:

protected function _initAutoload()
{
    Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_');
}
画▽骨i 2024-08-26 13:58:16

不确定这是否是你的问题,但我刚刚花了最后一天半的时间试图找出我自己的类似问题(第一次从 Windows 将其加载到 Linux 上)。事实证明我对图书馆的文件夹名称大小写一无所知。

/library
    /Tlib

与(在 *nix 上)不同

/library
    /tlib

类名通常是这样

class Tlib_FooMe {
 ...
}

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

Not sure if this is your problem, but I just spent the last day and half trying to figure out my own similar problem (first time loading it up on Linux from Windows). Turns out I was blind to my library's folder name case.

/library
    /Tlib

is not the same as (on *nix)

/library
    /tlib

Class name is typically this

class Tlib_FooMe {
 ...
}

Hope this helps someone who is similarly absentminded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文