Hi all.
I am trying to understand if the different patterns between some bits of code are intentional, eg:
\Laminas\Form\View\Helper\AbstractHelper::getDoctypeHelper()
protected function getDoctypeHelper(): Doctype
{
if ($this->doctypeHelper) {
return $this->doctypeHelper;
}
if ($this->view !== null && method_exists($this->view, 'plugin')) {
$this->doctypeHelper = $this->view->plugin('doctype');
}
if (! $this->doctypeHelper instanceof Doctype) {
$this->doctypeHelper = new Doctype();
}
return $this->doctypeHelper;
}
\Laminas\Form\View\Helper\FormSelect::getFormHiddenHelper():
protected function getFormHiddenHelper(): FormHidden
{
if (! $this->formHiddenHelper) {
if (method_exists($this->view, 'plugin')) {
$this->formHiddenHelper = $this->view->plugin('formhidden');
}
if (! $this->formHiddenHelper instanceof FormHidden) {
$this->formHiddenHelper = new FormHidden();
}
}
return $this->formHiddenHelper;
}
\Laminas\Form\View\Helper\AbstractFormDateSelect::getSelectElementHelper():
protected function getSelectElementHelper(): FormSelect
{
if (null !== $this->selectHelper) {
return $this->selectHelper;
}
if (method_exists($this->view, 'plugin')) {
$selectHelper = $this->view->plugin('formselect');
assert($selectHelper instanceof FormSelect);
$this->selectHelper = $selectHelper;
}
assert(null !== $this->selectHelper);
return $this->selectHelper;
}
AbstractHelper::getDoctypeHelper()
- will always return a Doctype().
FormSelect::getFormHiddenHelper()
- will throw a TypeError if $this->view is null,
- will return a retrieved class if $this->view->plugin() method exists
- will create a FormHidden() if $this->view doesn’t provide plugin() method
AbstractFormDateSelect::getSelectElementHelper()
- will throw a TypeError if $this->view is null
- will return a retrieved class if $this->view->plugin() method exists
- will NOT create a FormSelect() if $this->view doesn’t provide plugin() method
Is there a reason they don’t all apply the same pattern ie,
- have $this->view !== null && $this->view…
- create the required class as fallback if that condition isn’t true
?