{"id":36805,"date":"2016-11-14T11:04:54","date_gmt":"2016-11-14T16:04:54","guid":{"rendered":"https:\/\/tommcfarlin.com\/?p=36805"},"modified":"2016-11-14T11:18:08","modified_gmt":"2016-11-14T16:18:08","slug":"singletons-in-wordpress","status":"publish","type":"post","link":"https:\/\/tommcfarlin.com\/singletons-in-wordpress\/","title":{"rendered":"Use and Abuse Singletons in WordPress"},"content":{"rendered":"<p>For those who make a living by creating custom solutions for others using WordPress, you likely have your approach to how you structure the foundation of your typical plugin. And I think that&#8217;s a Good Thing\u2122.<\/p>\n<p>If you&#8217;ve read\u00a0<em>any<\/em> of my previous posts, you know that I generally prefer the object-oriented approach to building plugins so there are a number of patterns, common solutions, and architectural decisions that I try to employ from project-to-project.<\/p>\n<p>I&#8217;m not dogmatic about it, though:<\/p>\n<p style=\"padding-left: 30px;\">If something new comes along, I&#8217;m willing to give it a try. Similarly, if someone I&#8217;ve been using for a while is criticized, I&#8217;m willing to take a look but that doesn&#8217;t mean I&#8217;ll change it.<\/p>\n<p>In short, I need a lot of information to sell my on why I should change something that works, that&#8217;s maintainable, and that reads well for other developers.<\/p>\n<p>And one of the points of discussion that comes up periodically\u00a0is the idea of using Singletons in WordPress development.<\/p>\n<p><!--more--><\/p>\n<h2>Singletons in WordPress<\/h2>\n<p>I&#8217;ve talked about <a href=\"https:\/\/tommcfarlin.com\/singleton-pattern-in-wordpress\/\">the design pattern<\/a> (and others) <a href=\"https:\/\/tommcfarlin.com\/on-design-patterns-in-wordpress\/\">in previous posts<\/a>, and I&#8217;ve shared how to implement this particular pattern in your work.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-36806 size-full\" src=\"https:\/\/tommcfarlin.com\/wp-content\/uploads\/2016\/11\/Blog-Sketches-2.png\" alt=\"Singletons in WordPress: The pattern diagram.\" width=\"700\" height=\"647\" srcset=\"https:\/\/tommcfarlin.com\/wp-content\/uploads\/2016\/11\/Blog-Sketches-2.png 700w, https:\/\/tommcfarlin.com\/wp-content\/uploads\/2016\/11\/Blog-Sketches-2-300x277.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>Personally, I don&#8217;t consider it an antipattern. But there&#8217;s a caveat to that:<\/p>\n<p style=\"padding-left: 30px;\">I don&#8217;t consider it an antipattern when it&#8217;s used under very specific circumstances and it&#8217;s not used as a way to circumvent better object-oriented principles.<\/p>\n<p>When any of the above happens,\u00a0we&#8217;ve turned it into an antipattern. Secondly, I don&#8217;t want to be a person who&#8217;s spreading any kind of message that it&#8217;s always okay to use singletons in WordPress.<\/p>\n<p>It&#8217;s not. Please don&#8217;t do it.<\/p>\n<p>But as I said earlier, it takes a fair amount of information to change my mind when it comes to\u00a0changing something\u00a0<em>completely<\/em> when I&#8217;m not totally sold on the idea there&#8217;s a fault with a given topic. And singletons in WordPress is one of those.<\/p>\n<p><a href=\"https:\/\/twitter.com\/garyj\">Gary Jones<\/a>, however, a notable object-oriented expert and active WordPress community\u00a0member maintain\u00a0<a href=\"https:\/\/gist.github.com\/GaryJones\/8430080\">a gist of reasons<\/a>, posts, and third-party resources as to why we should avoid using singletons.<\/p>\n<p>You can star the gist <a href=\"https:\/\/gist.github.com\/GaryJones\/8430080\">here<\/a>, but here\u00a0are links to <em>some<\/em> of the articles that I think are worth reading:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.youtube.com\/watch?v=-FRm3VPhseI\">Google&#8217;s Clean Code Talks<\/a><\/li>\n<li><a href=\"http:\/\/www.phptherightway.com\/pages\/Design-Patterns.html\">You Should Be Wary When Using The Singleton Pattern<\/a><\/li>\n<li><a href=\"http:\/\/www.practicaldesignpatternsinphp.com\">Singletons in PHP: Why They Are Bad<\/a><\/li>\n<\/ul>\n<p>And there are <a href=\"https:\/\/gist.github.com\/GaryJones\/8430080\">many more resources<\/a>.<\/p>\n<p>Granted, it helps to understand the Singleton Pattern so I&#8217;m assuming that you do. The Wikipedia definition <a href=\"https:\/\/en.wikipedia.org\/wiki\/Singleton_pattern\">is sufficient<\/a>:<\/p>\n<blockquote><p>In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.<\/p><\/blockquote>\n<p>But perhaps an easier to way to explain it would:<\/p>\n<blockquote><p>A class that creates and returns a single instance of itself. Hence the name singleton.<\/p><\/blockquote>\n<p>Does it have its place? I think so. Is it overused? Yes. And that doesn&#8217;t mean I think we should avoid advanced strategies (like dependency injection) or anything like that.<\/p>\n<p>But I also don&#8217;t think that every problem that we need to tackle has to be done in a way that&#8217;s overkill for the problem at hand.<\/p>\n<p>So it&#8217;s important to be pragmatic.<\/p>\n<h2>So Singletons or Not?<\/h2>\n<p>Ultimately, the point of the post is two-fold:<\/p>\n<ol>\n<li>Educate yourself around what the singleton pattern is and determine how much you&#8217;re\u00a0using singletons in WordPress,<\/li>\n<li>Determine if there are better, alternative ways to implement a solution.<\/li>\n<\/ol>\n<p>In short, I&#8217;m not totally opposed to singletons in WordPress. At least not right now. But I do think there are far better patterns for more complex problems and it&#8217;s important to determine what works best when and where for a given problem.<\/p>\n<p>That seems clear enough, right? &#x1f642;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the points of discussion that comes up periodically is using Singletons in WordPress development. I&#8217;m not totally against it.<\/p>\n","protected":false},"author":30,"featured_media":0,"comment_status":"open","ping_status":"closed","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-36805","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\/36805","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=36805"}],"version-history":[{"count":0,"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/posts\/36805\/revisions"}],"wp:attachment":[{"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/media?parent=36805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/categories?post=36805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tommcfarlin.com\/wp-json\/wp\/v2\/tags?post=36805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}