开发者

ZF Autoloader to load ancestor and requested class

开发者 https://www.devze.com 2022-12-23 15:51 出处:网络
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 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.

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 { ... }

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 tra开发者_如何学运维nslation 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.

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

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

  • 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.

  • Are there extensions to the Zend Autoloader that do this?

  • 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.


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(), '');

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.


See the manual on Zend_Loader:

By default, the autoloader is configured to match the "Zend_" and "ZendX_" namespaces. If you have your own library code that uses your own namespace, you may register it with the autoloader using the registerNamespace() method.

$autoloader->registerNamespace('My_');

Note that the autoloader follows the ZF Naming Convention, so Zend_Foo_Bar will look in Zend/Foo/Bar.php.

However,

You can also register arbitrary autoloader callbacks, optionally with a specific namespace (or group of namespaces). Zend_Loader_Autoloader will attempt to match these first before using its internal autoloading mechanism.

$autoloader->pushAutoloader(array('ezcBase', 'autoload'), 'ezc');

Another way would be to create custom class Loader extending Zend_Loader and then register it with:

Zend_Loader::registerAutoload('My_Loader');

ZF will then use this autoloader instead of the default one.

0

精彩评论

暂无评论...
验证码 换一张
取 消