1

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
  • 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 idea Commented Jun 16, 2009 at 10:15

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.