ZF Autoloader 加载祖先和请求的类 [英] ZF Autoloader to load ancestor and requested class

查看:18
本文介绍了ZF Autoloader 加载祖先和请求的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Zend Framework 集成到现有应用程序中.我想将应用程序切换到 Zend 的自动加载机制以替换数十个 include() 语句.

I am integrating Zend Framework into an existing application. I want to switch the application over to Zend's autoloading mechanism to replace dozens of include() statements.

不过,我对自动加载机制有特定要求.请允许我详细说明.

I have a specific requirement for the autoloading mechanism, though. Allow me to elaborate.

现有应用程序使用一个核心库(独立于 ZF),例如:

The existing application uses a core library (independent from ZF), for example:

/Core/Library/authentication.php
/Core/Library/translation.php
/Core/Library/messages.php

这个核心库将始终保持不变,并为许多应用程序提供服务.该库包含诸如

this core library is to remain untouched at all times and serves a number of applications. The library contains classes like

class ancestor_authentication { ... }
class ancestor_translation { ... }
class ancestor_messages { ... }

应用程序中,还有一个Library目录:

in the application, there is also a Library directory:

/App/Library/authentication.php
/App/Library/translation.php
/App/Library/messages.php

这些包括扩展祖先类,并且是在应用程序中实际实例化的类.

these includes extend the ancestor classes and are the ones that actually get instantiated in the application.

class authentication extends ancestor_authentication { }
class translation extends ancestor_translation { }
class messages extends ancestor_messages { }

通常,这些类定义是空的.它们只是扩展它们的祖先并提供要实例化的类名.

usually, these class definitions are empty. They simply extend their ancestors and provide the class name to instantiate.

$authentication = new authentication();

此解决方案的目的是能够轻松自定义应用程序的各个方面,而无需修补核心库.

The purpose of this solution is to be able to easily customize aspects of the application without having to patch the core libraries.

现在,我需要的自动加载器必须了解这种结构.当请求 authentication 类的对象时,自动加载器必须:

Now, the autoloader I need would have to be aware of this structure. When an object of the class authentication is requested, the autoloader would have to:

1. load  /Core/Library/authentication.php
2. load  /App/Library/authentication.php

我目前的方法是创建一个自定义函数,并将其绑定到 Zend_Loader_Autoloader 以获得特定的命名空间前缀.

My current approach would be creating a custom function, and binding that to Zend_Loader_Autoloader for a specific namespace prefix.

  • 在我忽略的 Zend 中,是否已经有一种方法可以做到这一点?这个问题中接受的答案有点暗示存在,但这可能是只是措辞不当.

  • Is there already a way to do this in Zend that I am overlooking? The accepted answer in this question kind of implies there is, but that may be just a bad choice of wording.

是否有 Zend Autoloader 的扩展来执行此操作?

Are there extensions to the Zend Autoloader that do this?

您 - 我是 ZF 的新手 - 能想出一种符合框架精神的优雅方式来扩展具有此功能的自动加载器吗?我没有必要寻找现成的实现,一些指针(这应该是 xyz 方法的扩展,你会像这样调用......)已经足够了.

Can you - I am new to ZF - think of an elegant way, conforming with the spirit of the framework, of extending the Autoloader with this functionality? I'm not necessary looking for a ready-made implementation, some pointers (This should be an extension to the xyz method that you would call like this...) would already be enough.

推荐答案

扩展Gordon 已经指出,我会创建自己的自动加载器类来实现 Zend_Loader_Autoloader_Interface 并将其推送到 Zend_Loader_Autoloader-stack.

To extend what Gordon already pointed out, I'd create my own autoloader class that implements Zend_Loader_Autoloader_Interface and push that onto the Zend_Loader_Autoloader-stack.

class My_Autoloader implements Zend_Loader_Autoloader_Interface 
{
    public function autoload($class) 
    {
        // add your logic to find the required classes in here
    }
}

$autoloader = Zend_Loader_Autoloader::getInstance();
// pushAutoloader() or unshiftAutoloader() depending on where 
// you'd like to put your autoloader on the stack
// note that we leave the $namespace parameter empty
// as your classes don't share a common namespace
$autoloader->pushAutoloader(new My_Autoloader(), '');

我不会采用 Zend_Loader 方法,因为即使尚未弃用,新的​​ Zend_Loader_Autoloader 目前似乎是最佳实践.

I wouldn't go with the Zend_Loader approach as, even though not deprecated yet, the new Zend_Loader_Autoloader seems to be best practice currently.

这篇关于ZF Autoloader 加载祖先和请求的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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