views:

157

answers:

2

What is right way when using Zend_Form to create concrete application forms

1) Using "extends" and derived class

class MyForm extends Zend_Form {

 public function __construct() {
  $el = $this->createElement('text', 'el');
  $this->addElement($el);
  // ...
 }

}

2) Or using delegation/proxy pattern

class MyForm {

 private $_form;

 public function __construct() {
  $this->_form = new Zend_Form();
  $el = $this->createElement('text', 'el');
  $this->_form->addElement($el);
  // ...
 }

 public function __call($I_method, $I_params) {
   // ... forwarding calls to private delegate
 }

}
+1  A: 

I create Form classes as per this example and use a helper to instantiate them. The inspiration came from Matthew Weier O'Phinney, ZF project lead, so I'm happy to accept it as good practice.

David Caunt
thx for you example, i also use this approach to forms, but sometimes think that it may not good way. According to LISP principle: 1) Preconditions cannot be strengthened in a subclass. 2) Postconditions cannot be weakened in a subclass. I think when we overriding method in subclass, we change it behaviour and use condictions.
duganets
I'm more of a practical person than an academic, and while I understand the LSP, the subtle difference isn't enough to concern me. Your form still behaves like a Zend_Form, but it has some elements.
David Caunt
A: 

I'm using

My_Form extends Zend_Form

and then

My_Form_ConcreteImplementation extends My_Form

The My_Form class is used to set default decorators and stuff and the concrete impelemntation adds elements and handles the business if necessary. It makes it DRY and that's what you need. No need to keep up to academic principles :P

Tomáš Fejfar
if i understand correctly, MyForm - middle layer for all forms in your application, so you have choosen 1), thx, for your example
duganets