Search code examples
zend-frameworkzend-framework-modules

Zend Framework 1.10 Override Class Call?


I think I am trying to be overly clever. If it works, yay. If not, no worries.

I want my application to be as modular as possible. At the moment I have just a products module but from experience I know I might want a categories module as well, in which products will be children of. Whilst the categories module does not exist I want calls to, for example, Products_Forms_NewProduct to load that form. When the categories module is dropped into the application I want it to be automatically detected and forward any calls for, for example, Products_Forms_NewProduct to Categories_Forms_NewProduct.

How do I go about this?


Solution

  • You're looking for dependency injection IMO. But I'd suggest having a class that would modify the form appropriately. Like a form-plugin. This is no case of extension, but rather composition. I'd go with something alon those lines:

    // say in Form_NewProduct's init()
    $this->addPlugin(new Form_Plugin_Category());
    $this->addSubform(new Form_NewProduct_ProductSubform());
    
    //in plugin or in form directly
    if (class_exists('Form_NewProduct_CategorySubform')) {
        $this->_form->addSubform(new Form_NewProduct_CategorySubform());
    }