{"id":23171,"date":"2014-04-30T08:14:00","date_gmt":"2014-04-30T12:14:00","guid":{"rendered":"http:\/\/tommcfarlin.com\/?p=23171"},"modified":"2014-04-30T08:14:00","modified_gmt":"2014-04-30T12:14:00","slug":"singleton-pattern-in-wordpress","status":"publish","type":"post","link":"https:\/\/tommcfarlin.com\/singleton-pattern-in-wordpress\/","title":{"rendered":"Of Libraries and The Singleton Pattern in WordPress"},"content":{"rendered":"<p>Over the past couple of years, I&#8217;ve written a number of articles about using design patterns in WordPress.\u00a0One that I&#8217;ve written about a number of times\u00a0has to do with the <a title=\"Singleton Pattern\" href=\"http:\/\/en.wikipedia.org\/wiki\/Singleton_pattern\" target=\"_blank\">Singleton Pattern<\/a>.<\/p>\n<p>Although I&#8217;m not really expecting anyone to spend time reading through the following list, some of\u00a0the articles and\/or comments that discuss the pattern include:<\/p>\n<ul>\n<li><a title=\"An Introduction to the Singleton Pattern\" href=\"http:\/\/code.tutsplus.com\/tutorials\/design-patterns-in-wordpress-the-singleton-pattern--wp-31621\" target=\"_blank\">An Introduction to the Singleton Pattern<\/a><\/li>\n<li><a title=\"Properly Instantiate a WordPress Plugin\" href=\"http:\/\/tommcfarlin.com\/instantiate-a-wordpress-plugin\/#comments\" target=\"_blank\">Properly Instantiate a WordPress Plugin<\/a><\/li>\n<li><a title=\"Instantiating WordPress Plugins\" href=\"http:\/\/tommcfarlin.com\/instantiating-wordpress-plugins\/\" target=\"_blank\">Instantiating WordPress Plugins<\/a><\/li>\n<li><a title=\"Object-Oriented WordPress Development\" href=\"http:\/\/tommcfarlin.com\/object-oriented-wordpress-plugin\/\" target=\"_blank\">Object-Oriented WordPress Plugin Development<\/a><\/li>\n<\/ul>\n<p>The funny thing about blogging is that when you&#8217;ve written something two years, one year, or even six months ago, you may not feel the same way as you did when you initially wrote the post. And that&#8217;s fine &#8211; as far as I&#8217;m concerned, it&#8217;s an indication of professional growth (or\u00a0just\u00a0getting better at what you do).<\/p>\n<p>So with that said, I don&#8217;t use the singleton pattern as much as I used to the reasons for which are a topic for another discussion. Instead, I was recently talking with <a title=\"Michael Novotny\" href=\"http:\/\/twitter.com\/manovotny\/\" target=\"_blank\">a good friend<\/a> about implementing the singleton pattern within the context of a WordPress plugin and the discussion really came down to this:<\/p>\n<p style=\"padding-left: 30px;\">Should plugins that implement the singleton pattern be instantiated in a hook?<\/p>\n<p>Make sense?<!--more--><\/p>\n<h2>The Singleton Pattern in WordPress<\/h2>\n<p>For those of you who aren&#8217;t familiar with the Singleton Pattern, suffice it to say that it&#8217;s a design pattern that allows only a\u00a0<em>single<\/em> instance of a class to be created.<\/p>\n<p>If an instance\u00a0doesn&#8217;t exist, it creates\u00a0it and manages a reference to it internally, and returns it; otherwise, it simply returns the instance that was previously created.<\/p>\n<p>It&#8217;s probably the simplest design pattern.<\/p>\n<p>Though I&#8217;m not as much of a fan of this implementation as I once was, I still have a couple of projects on GitHub that show <a title=\"The Singleton Pattern in Page Template Example\" href=\"https:\/\/github.com\/tommcfarlin\/page-template-example\/blob\/master\/class-page-template-example.php\" target=\"_blank\">how to implement it<\/a>. Eventually I&#8217;d love to update them but, you know, newer projects, other\u00a0work, etc.<\/p>\n<p>Anyway, all that to say that if you aren&#8217;t familiar with how the design pattern works, there are a few articles and examples mentioned earlier to get you up to speed.<\/p>\n<h3>A Formula for Object-Oriented WordPress Plugins<\/h3>\n<p>So, as I mentioned, a friend and I were talking about creating a plugin that implements the Singleton Pattern and how this should be used in comparison to other WordPress plugins.<\/p>\n<p>Specifically, whenever anyone considers creating a WordPress plugin &#8211; especially using object-oriented techniques &#8211; they&#8217;re likely think of three things:<\/p>\n<ol>\n<li>Creating a class<\/li>\n<li>Creating a constructor<\/li>\n<li>Adding hooks into the constructor<\/li>\n<li>Setting the callbacks for the hooks as instance methods<\/li>\n<\/ol>\n<p>This is the generic standard format (and stands to be improved) that&#8217;s seen most often.<\/p>\n<p>From there, let&#8217;s that that you have a plugin that offers a collection of utility methods\u00a0<em>and<\/em> it implements the Singleton Pattern. This raises the question: Should a class that implements the Single Pattern be instantiated within a hook such as <code>plugins_loaded<\/code> (or any other hook, for that matter)?<\/p>\n<p>My\u00a0short answer: No. I don&#8217;t think so.<\/p>\n<h3>Don&#8217;t Instantiate it <em>There<\/em><\/h3>\n<p>You see, the whole idea behind the Singleton Pattern is that one-and-only-one instance of the class that implements that pattern\u00a0will exist. To that end it doesn&#8217;t matter if you\u00a0instantiate it with a hook in the context of the plugin, or if you attempt to get an instance to the plugin by simply calling the <code>get_instance<\/code> method at some point in your theme (or in another plugin).<\/p>\n<p>All you&#8217;re\u00a0<em>really<\/em> doing is determining a point at which the plugin is first instantiated, and the amount of work required to do so is usually so minimal &#8211; at least for classes that implement this pattern &#8211; that it&#8217;s\u00a0you&#8217;re likely fine instantiating it the first time that you need it rather than using a hook in the plugin.<\/p>\n<p>But this raises an interesting point: Assuming that you\u00a0<em>are<\/em>\u00a0going to implement the\u00a0Singleton Pattern, perhaps it&#8217;s worth considering on the type of functions that the class is going to encapsulate.<\/p>\n<p>If you&#8217;re looking to do some advanced object-oriented programming, then you&#8217;re likely not going to be using the Singleton Pattern as you&#8217;d be better off with static methods or even, even better, just a collection of functions.<\/p>\n<p>This brings us to defining the class a library. In traditional programming sense (for lack of a better term), this means that it&#8217;s a collection, or a package of functions, that provide a clean interface for related\u00a0operations.<\/p>\n<p>And if we&#8217;re going to go that far, then perhaps it&#8217;s fair to say that all libraries are plugins, but not all plugins are libraries.<\/p>\n<p>Then again, not all classes that implement the Singleton Pattern are libraries, right?<\/p>\n<p>To clarify, the qualifying factor of if the class is a library is not whether or it implements a design pattern or not, but if it&#8217;s a class that encapsulates a group of related functions. Remember, in WordPress, we aren&#8217;t locked into using object-oriented programming anymore than we are locked into using procedural programming, so we could just as easily create a library of related with the same prefix.<\/p>\n<p>This feels like it&#8217;d getting a bit muddled, doesn&#8217;t it?<\/p>\n<h3>Wait, What?<\/h3>\n<p>To pull it back and keep it simple, I&#8217;d summarize it like this:<\/p>\n<ul>\n<li>If you&#8217;re creating a class that provides a clean interface to\u00a0a set of related functions,<\/li>\n<li>And if your class implements the Singleton Pattern,<\/li>\n<li>Then you&#8217;ve created a library (which\u00a0can be activated as a plugin)<\/li>\n<li>But you do not need to get an instance of the class in the context of any hook.<\/li>\n<\/ul>\n<p>Instead, you simply activate the plugin and then use it throughout your code as needed.<\/p>\n<p>Obviously, this approach is more geared towards other developers are who are looking for some type of utility to use in their plugin or in their theme or WordPress-based application, but the point remains that the next time you&#8217;re looking at implementing the Singleton Pattern in WordPress, ask yourself if it&#8217;s presenting itself as a library.<\/p>\n<p>If so, then it may be a fair\u00a0implementation; otherwise,\u00a0consider an alternative implementation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the past couple of years, I&#8217;ve written a number of articles about using design patterns in WordPress.\u00a0One that I&#8217;ve written about a number of times\u00a0has to do with the Singleton Pattern. Although I&#8217;m not really expecting anyone to spend time reading through the following list, some of\u00a0the articles and\/or comments that discuss the pattern [&hellip;]<\/p>\n","protected":false},"author":30,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[154],"class_list":["post-23171","post","type-post","status-publish","format-standard","hentry","category-articles","tag-wordpress","post-preview"],"_links":{"self":[{"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/posts\/23171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/users\/30"}],"replies":[{"embeddable":true,"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/comments?post=23171"}],"version-history":[{"count":0,"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/posts\/23171\/revisions"}],"wp:attachment":[{"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/media?parent=23171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/categories?post=23171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/tags?post=23171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}