Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

TemplateWrapper getter #6679

Closed

Conversation

backnight
Copy link

Added the call of getTemplateWrapper() instead of directly passing the protected var.

weierophinney and others added 2 commits August 20, 2014 08:18
Added the call of getTemplateWrapper() instead of directly passing the protected var.
@samsonasik
Copy link
Contributor

No, it's already right. We call it in same class

@Martin-P
Copy link
Contributor

No, it's already right. We call it in same class

For consistency I agree with @backnight. If you look at the class as a whole almost everywhere the getters are used. Personally I prefer using the getters whenever they are available. Perhaps this is something for the ZF2 coding standards?

@samsonasik
Copy link
Contributor

using getter is only make sense when working from outside class, and it faster too, except getter do logic rather than only return the value. you can take a look at Zend\Mvc\Application.php that I replaced the getter with call directly the properties.

Warm regards,

Abdul Malik Ikhsan

Pada 22 Sep 2014, pukul 14.25, Martin-P notifications@github.com menulis:

No, it's already right. We call it in same class

For consistency I agree with @backnight. If you look at the class as a whole almost everywhere the getters are used. Personally I prefer using the getters whenever they are available. Perhaps this is something for the ZF2 coding standards?


Reply to this email directly or view it on GitHub.

@Martin-P
Copy link
Contributor

except getter do logic rather than only return the value

That is the main reason for me to use getters whenever they are available, you never know if you need to add some logic to a getter. IMHO the ease of code change when using getters whenever they are available supersedes the minor performance gain (benchmark?) for directly accessing properties.

In this case getters are already available and therefore it seems logical to me to use them.

@backnight
Copy link
Author

I added the getter because I needed to do some logic.
Take a look at this example :

  • I extend the Zend\Form\View\Helper\FormCollection with a personal form view helper.
  • In this new class, I want the templateWrapper var to be translated :

class BaseCollection extends FormCollection
{
protected $templateWrapper = '< span data-template="%s" class="collection-add-subelement">%s< /span>';

public function getTemplateWrapper()
{
    return sprintf(parent::getTemplateWrapper(), "%s", $this->getView()->translate('Add'));
}

}

The templateWrapper is not just an html element containing the template, I use it also to add the template to the collection, like a button.

Zend is not using the getter, so the translation is not occurring that why I added the call of the getter instead of the var directly.

@samsonasik
Copy link
Contributor

at that case, I personally override the setTemplateWrapper instead.

@backnight
Copy link
Author

The $templateWrapper var should be kept as a template (with "%s"). It's easier for further logics and reading.
Doing the translation in the setter is not the right way IMO because :

  • You have to add the __construct() function which calls the setter and the $templateWrapper var is not a template anymore (no more flexibility because %s has been replaced)...
  • You are breaking with the Zend logic (template defined in the var directly).

@Ocramius
Copy link
Member

I'm fine with the introduced change, as it allows for easier overrides, but I'd still ask for a test case validating the call. This may be done by mocking the SUT and replacing the getter at runtime.

@Ocramius
Copy link
Member

Ocramius commented Dec 5, 2014

Will merge as-is: can't spend time testing this via reflection ATM, and it would be a minor BC break to change this afterwards anyway, so it doesn't need to be protected against regressions.

Ocramius added a commit that referenced this pull request Dec 5, 2014
…instead-of-direct-property-access' into develop

Close #6679
@Ocramius
Copy link
Member

Ocramius commented Dec 5, 2014

Merged, thanks @backnight:

develop: 0e0ac62

@Ocramius Ocramius closed this Dec 5, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants