4066

ZF Autoloader to load ancestor and requested class

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

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.

    <li>

    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.

    </li> <li>

    Are there extensions to the Zend Autoloader that do this?

    </li> <li>

    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.

    </li> </ul>

    Answer1:

    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.

    Answer2:

    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.

Recommend

  • REACT: Add highlighted border around selected image [closed]
  • Report Schedule
  • What is the purpose of Google Cloud Messaging
  • DB2 JDBC Create clob error
  • ZF Autoloader to load ancestor and requested class
  • Swift 3: How to reverse a transparent Navigation Bar?
  • Shake iPhone to open my app. Possible?
  • Delphi: how to compose an email in Outlook without using MAPI?
  • Quote original message in a reply using mailkit
  • Dropping support for JRE 1.3
  • Customize google placepicker colors for android
  • JPS useBean with HashMap
  • Visual Studio 2017 ASP.Net Publish Self-Contained Dot Net Core App
  • Resizing ToolStripButtons to fit complete BackGround image
  • Cannot instantiate the type Image java?
  • “Complex Header” not responsive in current DataTables.net build?
  • How to set `secure` and `httpOnly` for Plones `__ac` cookie?
  • How to initialize context? [closed]
  • Jquery UI Sortable, move item automatically
  • include dlls in visual studio c++ 2008
  • react split panel resize
  • Migration to HRD - How to convert string-encoded keys to new application
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • Adding a model to a Marionette CollectionView's collection doesn't trigger onItemAdd callb
  • apply a javascript function to draggable copy
  • Aptana 3 remove bundle (jquery)
  • netsh acl setting (need alternative method - registry settings?)
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • What's the purpose of QString?
  • Zurb Foundation _global.scss meta styles for js?
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • Join two tables and save into third-sql
  • How can I use Kendo UI with Razor?
  • How to make Safari send if-modified-since header?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Why joiner is not used after Sequence generator or Update statergy