Zend_Form and OOP design patterns

Go To StackoverFlow.com


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');
  // ...


2) Or using delegation/proxy pattern

class MyForm {

 private $_form;

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

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

2009-06-16 09:45
by duganets
2) is not a prox - dfa 2009-06-16 09:50
why not proxy? real subject is hidden private member, and receives messages from MyForm, may be not full expanded class implementation, but i try to show basic ide - duganets 2009-06-16 10:15


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.

2009-06-16 09:56
by David Snabel-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 2009-06-16 10:21
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 Snabel-Caunt 2009-06-16 11:02


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

2009-06-17 16:51
by Tomáš Fejfar
if i understand correctly, MyForm - middle layer for all forms in your application, so you have choosen 1), thx, for your exampl - duganets 2009-06-18 08:22