Closed timkelty closed 5 years ago
This is a convention that many plugin developers, myself included, have adopted. It is purely for readability purposes (and brevity) and follows the Craft::$app
convention. Compare how you call the Blitz plugin versus the Commerce plugin, for example:
use putyourlightson\blitz\Blitz;
use craft\commerce\Plugin;
...
// Call the Blitz plugin
Blitz::$plugin
// Call the Commerce plugin
Plugin::getInstance()
@putyourlightson Gotcha, makes sense.
I guess personally I'd lobby for simplicity and keeping "preferences" like this out of the generator.
I don't really see a win between Blitz::$plugin
and Blitz::getInstance()
, but that's just me :)
I guess the argument could be made that calling the getInstance()
method several times is less performant than calling it once, when $plugin
is first defined. But it is an insignificant optimisation, so really just a matter of preference.
@timkelty I've waffled on this myself. I think we can all agree that Blitz::
is better than Plugin::
, but then (as Ben said) it's just personal preference as to whether you use ::$plugin
or ::getInstance()
.
When in doubt, I tend to mimic what Craft itself is doing. And since Craft uses ::$app
, it makes a good case for the ::$plugin
convention.
Consistency might also argue this should be either MyModule::$module
, or ::$instance
for both Plugin
and Module
…
…but I'll just use getInstance()
🕺
I'd love to use MyModule::$module
, but Yii already has that reserved.
Both
Plugin::$plugin
andModule::$instance
get set to$this
in theinit
methods.Yii already provides a
Module::getInstance()
. Seems like unnecessary overhead to include this prop.