views:

129

answers:

2

i have created my own Zend_Form class currently just to add some helper functions and setdefault decorators.

now what i want to do is add some defaults (setMethod('post')) and add some elements (if u know the honeypot method to prevent spam bots.) how do i do it? in the __construct()?

+2  A: 

If you will read the __construct of Zend_Form you will see it is calling the empty method init().
So you have two choices:

  1. create your own construct and do not forget to call the parent construct
  2. (recommended) put what ever you want in the init() method. That way you will be sure that everything was instantiated correctly.

the init() is something consistent to the entire framework (which ever class you want to extend).

class My_Super_Form extends Zend_Form{
   protected function init(){
     //here I do what ever I want, and I am sure it will be called in the instantiation.
   }
}
Itay Moav
the reason why i think of using the construct is because i want to create an "abstract" zend form class (reusability) where all my other forms will extend. i mannaged to make it work with `__construct()` and i called `parent::construct()` at the start too
jiewmeng
create the abstract which will inherit the Zend_Form and you inherit the abstract. The only reason for that is that each upgrade of the system you will have to touch the library if you do that. (or I did not understand what you did).
Itay Moav
+2  A: 

Overwrite the contructor should be fine.

class Your_Form extends Zend_Form
{
    public function __construct($options = null)
    {
        $this->setMethod('POST');
        $this->addElement('hidden', 'hash', array('ignore' => true));
        parent::__construct($options);
    }

So your other Forms can extend Your_Form and call init(), so it stays consistent.

class Model_Form_Login extends Your_Form
{
    public function init()
    {
         $this->addElement('input', 'username');
         ...

If you overwrite the init()-Method you don't have to call the parent::__construct()...

class Your_Form extends Zend_Form
{
    public function init()
    {
        $this->setMethod('POST');
        $this->addElement('hidden', 'hash', array('ignore' => true));

    }

... but all your extending Forms have to call parent::init() like so

class Model_Form_Login extends Your_Form
{
    public function init()
    {
         $this->addElement('input', 'username');
         ...
         parent::init();
Benjamin Cremer