I am working on a WordPress Plugin and am trying to ensure best practices. I have two classes, my plugin class “Jargonaut” which is required and then another class called “Dictionary” which is included with require_once() into my main plugin file.
Most of the code in the Jargonaut class addresses initialization and provides controller-like functionality but much of it is highly dependent upon using the Dictionary object (i.e. tightly coupled from my understanding of the term). I wish to keep the Dictionary class separated as it is acting more like a model (in MVC architecture) and interfaces with my database.
I see a lot of gray area in the tight vs. loose coupling and am having a hard time deciding how much is too much?
If your plugin needs the dictionary object, it has to ask for it:
You now have loosely coupled your plugin with the
Dictionary, the plugin class is not responsible any longer to create the Dictionary for itself, because it’s injected. It takes what it needs.So how would that work? The plugin needs to be created somewhere, so this needs a factory. The factory build method knows what your plugin needs:
As this is wordpress we know that the bootstrapping of the plugin is done by including the plugin file. So at it’s beginning, the plugin needs to be created. As includes are done in the global scope we want to preserve the plugin object in memory but without being available as a global variable probably. This does not prevent you from creating more than one plugin instance, but it will ensure that when wordpress initializes your plugin (loads your plugin), it will make only use of that single instance. This can be done by making the plugin factory some additional function:
Take care here, that the only usage of the static class member variable is only to ensure that the plugin stays in memory. It technically is a global variable we normally want to prevent, however, the instance needs to be stored somewhere, so here it is (I changed this to public because it is a global variable and it shouldn’t be shy about it. Having a public can help in some circumstances in which private or protected are too restrictive. Also it shouldn’t be a problem. If it is a problem, there is another problem that should be fixed first).
This basically decouples your plugin code from wordpress itself. You might want to also create a class that offers and interface to any wordpress function you’re making use of, so you’re not bound to these functions directly and your plugin code stays clean and loosely coupled to wordpress itself.
Then add it as a dependency again if your plugin needs the
WordPressSystemto perform tasks (which normally is the case):So to finally wrap this up, only the plugin php file is needed:
The bootstrap method can also take care of registering an autoloader or do the requires.
Hope this is useful.