I need to create a few custom form elements (with custom view helpers) in my Zend Framework application. The problem is that they are each so similar. I want to create a base view helper class that each of these can extend and an abstract function that I will require is implemented.
Solution:
So if my Picker
element was the abstract class and ContactPicker
and OrganizationPicker
were the extending classes...
The form element:
class My_Form_Element_ContactPicker extends My_Form_Element_Picker
{
/**
* Default form view helper to use for rendering
* @var string
*/
public $helper = "contactPickerElement";
}
The view helper:
class My_View_Helper_ContactPickerElement extends My_View_Helper_PickerElement
{
public function contactPickerElement($name, $value = null, $attribs = null)
{
// I don't need to do anything in this function.
// I only need the parent to do all the work.
return parent::pickerElement($name, $value, $attribs);
}
protected function myAbstractFunctionThatMustBeImplemented()
{
// This function will do all the work specific to this extending class.
$model = new ContactModel();
return $model->foobar;
}
}
And here's the abstract view helper:
abstract class Evanta_View_Helper_PickerElement extends Zend_View_Helper_FormElement
{
/**
* This function would have been called automatically, but since it's being extended...
* Any extending classes must remember to manually call this function
*/
public function modalPickerElement($name, $value = null, $attribs = null)
{
$html = 'My picker element HTML';
return $html;
}
/**
* This function must be implemented by any extending classes
*/
abstract protected function myAbstractFunctionThatMustBeImplemented();
}