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

Change self to static to allow to easily override format patterns in subclasses #191

Closed
netbrothers-tr opened this issue Feb 8, 2017 · 4 comments
Assignees

Comments

@netbrothers-tr
Copy link
Contributor

Some (or all?) format patterns like this one

protected static $horizontalLayoutFormat = '<div class="%s">%s</div>';

are accessed within their class via self as in this line for example. However, because of late static binding you cannot really override those patterns in a subclass.

Let's say you have a subclass like this without overriding the render function.

class MyFormRow extends TwbBundleFormRow
{
    protected static $horizontalLayoutFormat = '<div class="%s" custom-attr="val">%s</div>';
}

This actually doesn't work at the moment. But it would be very easy to achieve it by changing the calls to those properties like this.

static::$horizontalLayoutFormat // instead of self::$horizontalLayoutFormat

It matches the PHP requirements (>=5.3) and without them being called with static I don't see a reason for those properties to be protected in the first place. This way it would be very nice and easy to subclass the bundle classes because sometimes it's just enough to change those patterns a little bit. Gives you a lot of flexibility.

@netbrothers-tr
Copy link
Contributor Author

For the sake of completeness: One can work around this issue like so.

class MyFormRow extends TwbBundleFormRow
{
    public function render(\Zend\Form\ElementInterface $oElement, $sLabelPosition = null)
    {
        self::$horizontalLayoutFormat = '<div class="%s" custom-attr="val">%s</div>';
        return parent::render($oElement, $sLabelPosition);
    }
}

@neilime
Copy link
Owner

neilime commented Feb 8, 2017

Hi,

Please can you create a pull request ?

@neilime neilime self-assigned this Feb 8, 2017
@netbrothers-tr
Copy link
Contributor Author

Thanks @neilime , I will get active about it. I assume I fork the master branch for that matter?

netbrothers-tr added a commit to netbrothers-tr/zf2-twb-bundle that referenced this issue Feb 9, 2017
neilime added a commit that referenced this issue Feb 9, 2017
Adressing #191 making all format patterns overrideable.
@neilime
Copy link
Owner

neilime commented Feb 9, 2017

PR merged, thank you !

@neilime neilime closed this as completed Feb 9, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants