views:

98

answers:

2

Ok, I think I did my due diligence here. I couldn't find any reference on how to use a parent form element in a subclassed form. May be because it's obvious to everyone but me. It's got me stumped. This is what I tried.

At first, within my form constructor I called

parent::__construct($options = null);

then accessed the parent elements like this

$type = parent::setName($this->type);

The problem was that ALL the parent form elements would display whether explicitly called or not. Someone said, "don't use __construct(), use the init() function instead. So I changed the constructor to init(), commented out the parent constructor, then ran the form. It bombed saying it couldn't pass an empty value for setName(). I commented out all the seName() calls and the form ran, but only displayed the elements instantiated in the subclassed form.

My question is this: If I don't use the parent constructor, how do i get and use the parent's form elements?

Solved: Since the constructor was switched to init, the call to the parent also needed to be switched. Easy for someone with php background. Not so much for one who doesn't.

Use

parent::init();
A: 

You should learn OOP principles first. Obviously you have no understanding of it whatsoever. You need to call parent::init() in you Form_Class::init() method as you wrote, but why? Because otherwise the parent method is not called and is overriden by the From_Class method.

Other thing is that when you have a parent class "SuperForm" with input and submit, then your "SuperForm_Subclass" would have the same elements assigned. There is no need to use "parent::*" to access element (only exception would be if you used static SuperForm variable to store them - which makes no sense).

You can easily use $this->inputElement and $this->submitElement inside your SuperForm_Subclass like you would in the SuperForm class.

In your example you could used the __contruct() as good, but with the same condition of calling the parent constructor. You would be able to access elements generated there too...

Tomáš Fejfar
You should learn some civility and empathy because obviously you have no understanding of those terms whatsoever. Your proposed solution is completely overshadowed by your arrogance.Not everyone enters the programming realm having suckled at the teat of formal training. I come to this forum seeking solutions. If all you want to do is denigrate those who know less than you, just shut the hell up and keep it to yourself.
Mike
When frameworks emerged, many users tend to think they can do things in PHP without any knowledge of any principles. I'm not happy with that fact. I spend quite some time helping others solve their programming problems. I keen to help anyone. But I tend to overreact when someone asks a question in a way that it's obvious, that he has no general knowledge and try to do some "big things". I don't think my reaction was in any way rude. I'm sorry, if the fact, that you should learn basics first hurt your feeling. Than, please, take my answer as a good advice with no hatred.
Tomáš Fejfar
A: 

Solved: Since the constructor was switched to init, the call to the parent also needed to be switched. Easy for someone with php background. Not so much for one who doesn't.

Use

parent::init();

Mike