One of the things that Pippin, Norcross, and I have been talking about during the course of building Comments Not Replied to is the best practices for instantiating WordPress Plugins.
Specifically, we’ve gone from simply creating an instance of the plugin, to storing it within the PHP
$GLOBALS variable, as well and then debating whether or not to implement the plugin as a singleton.
There’s more to this that I’ll cover in a follow-up post, but the most significant thing worth sharing in this post is why we’re discussing how to instantiate our plugin.
Wait, What’s Instantiation?
Generally speaking, I think programmers use a lot of big words to describe a simple concept, but I’ll cover that in a later article.
For those who aren’t aware, instantiation is basically the act of generating an actual instance of a class (instance, get it)? Whenever you do:
You’re instantiating a class. In typical representing, you end up storing a reference to the class in a variable:
$my_plugin = new Plugin_Name();
In this latter case, we do this for two reasons:
- We’re able to reference the functions and properties of the class.
- We’re able to de-construct, nullify the reference, release the resources (whatever you want to call it)
But you lose all of that ability in the the first example because the class is being created without being bound to a reference.
So What’s The Problem?
In a sense, you’ve created an orphaned object because you have no way to access it, or destroy it for that matter.
This can be problematic for a number of reasons:
- You may wish to kill a reference to the object sometime during its lifetime in the context of another plugin or theme
- You may wish to have access to its functions outside of the Plugin API to add, change, or modify its state
- …and so on
So in order to do this, there’s a simple fix. Instantiate the object like this:
$GLOBALS['my-plugin'] = new Plugin_Name();
Here, we’re storing a reference to the plugin the PHP
$GLOBALS collection that will allow us to access it at any place in the application.
Thanks to some of the comments below, I want to make it clear that there are two things that you need to be aware of when using this approach:
- Make sure that the key to which you’re assigning this instance doesn’t already exist.
- Understand exactly what you’re doing – if you’re unfamiliar with the
$GLOBALScollection or why messing with it in a larger context can be dangerous, opt not to do it.
There are other strategies for storing the reference that will be covered in future posts.
Anyway, an example of how to, say, clear this particular reference would be to do something like this:
unset( $GLOBALS['my-plugin'] );
I’m not necessarily saying you should do this. It’s simply showing an example of how you can take advantage of maintaining a reference to the object.
There’s More Than One Way…
As with most things in programming, there are other ways to go about creating objects, maintaining references to them, and even managing how objects are related to one another.
Those are clearly the topics for other posts, but I hope to get to them soon.