Zend AutoLoad 自定义目录?

问题描述:

我认为自动加载内置于 Zend 中,因此如果您尝试实例化一个类,它会使用类名来尝试查找该类的文件所在的位置.

I thought autoloading was build into Zend so that if you tried to instantiate a class it would use the class name to try and find where the class's file was located.

我希望能够使用自动加载器在我的应用程序根目录中加载 DTO 目录.我想我可以为文件/application/dtos/myClass.php

I want to be able to load a DTO directory in my application's root directory using autoloader. I thought I could do something like this Application_DTO_MyClass for the file /application/dtos/myClass.php

我试过谷歌搜索,但没有找到任何有用的东西.有关执行此操作的最佳方法的任何提示吗?

I've tried googling it but haven't found anything useful. Any tips on the best way to do this?

这里有几个选项可供您选择,具体取决于您是要在 Application/Models 的子目录中创建模型还是在您自己的命名空间"中创建它们' 作为 library 的子目录.我假设您正在使用 推荐的 Zend 框架目录结构.

There are a couple of options open to you here depending on whether you want to create models in a subdirectory of Application/Models or create them in your own 'namespace' as a subdirectory of library. I am assuming you are using the recommended directory structure for Zend Framework.

要在 Application/Models 的子目录中创建模型,首先创建您的目录;在您的情况下,应用程序/模型/Dto 将是我的建议.

For creating models in a sub directory of Application/Models, first create your directory; in your case Application/Models/Dto would be my recommendation.

在该目录中创建 Myclass.php,其中将包含:-

In that directory create Myclass.php that would contain:-

class Application_Model_Dto_Myclass
{
    public function __construct()
    {
        //class stuff
    }
}

你会这样实例化:-

$myObject = new Application_Model_Dto_Myclass();

如果你想在你自己的命名空间"中创建你的类作为 library 的子目录,那么首先创建目录 library/Dto,然后再次创建文件 Myclass.php,它看起来像这样:-

If you want to create your classes in your own 'namespace' as a subdirectory of library then first create the directory library/Dto and, again create the file Myclass.php, which would look like this:-

class Dto_Myclass
{
    public function __construct()
    {
        //class stuff
    }
}

您必须注册这个命名空间";我建议在您的 application.ini 中添加以下行:

You will have to register this 'namespace'; I would recommend doing that in your application.ini by adding the line:

autoloadernamespaces[] = "Dto_"

你会这样实例化这个类:-

You would instantiate this class thus:-

$myObject = new Dto_Myclass();

我不知道为什么你不能通过谷歌找到这个,但是你会在 Zend 框架程序员参考指南.我还发现 ZF 代码是了解其工作原理的绝佳资源.

I'm not sure why you couldn't find this out through google, but you will find all this and more in the Zend Framework Programmers' Reference Guide. I also find the ZF code to be an excellent resource for working out how it all works.