Pragmatic is now Angry Creative.

We’re extremely excited to announce that Pragmatic is merging with Angry Creative, creating a multinational agency experience with the strengths of both companies.

Home / Singletons: What are they and When to use them

Singletons: What are they and When to use them

Singletons are one of many design patterns that developers employ to solve common problems. In this three-part series, we will look at what they are, when to use them, some different types of singletons, and how to implement them yourself.

This post is at an intermediate level as it is quite conceptual but needs very little code knowledge. Although this post has examples in PHP, the concepts it describes apply to all object-oriented programming languages.

The next post in this series is available here: Singletons: Two ways about it.

What Are Singletons

The Problem

In any software system, we need to store data. Namely program state, in variables. For important pieces of data, we might want them to to be accessible to many different parts of the system, at many different times. Typically the simple answer to this is to make some global variables.

Global variables get a lot of stick online, and one of the prevalent reasons is to do with their structure.

Say you want to store quite a bit of data about the current web request, for example:

  • The time the connection was made
  • The duration of the request so far
  • The I.P. address of the requester
  • An array of sanitised POST and GET parameters
  • E.T.C.

You are either going to end up with a lot of global primitives, which will get cluttered very fast, or one global object. However, with a global object, if a developer writes:

$request_info = new RequestInfo();

instead of

global $request_info;

They are going to end up with a blank copy of your RequestInfo Object!

Enter Singletons

Wouldn’t it be nice if we could have a class, like our RequestInfo, but there can only ever be one of them. No matter how hard the pesky developers try, they will always end up with the original RequestInfo that you created for them.

That’s what a singleton is, it’s an object, that you can only ever have one of.

Creating a class that can only be instantiated once sounds like the opposite of what a class is meant for, but Singletons persist as an elegant way of storing global (or sometimes even scope specific) data, bundled into a convenient package.

Yay, let’s use Singletons for EVERYTHING

Is certainly what I said when I first heard about them. But you don’t want to become the fabled ‘man with a hammer‘ to which everything is a nail.

Let’s think about a couple of WordPress global variables, and decide whether they would be better as Singletons.

The Global $post Object

The global post object may seem like a great candidate at first:

  • It’s global
  • It’s an object

However, it’s very quick to see why we would not want to make WP_Post a Singleton:

global $post;
$related_post_id = get_post_meta( $post->ID, ‘related_post’, true );
$related_post = get_post( $related_post_id );

The above snippet simply loads a post ID from the global post’s meta and then gets a post object for that related post ID. In three lines of code, we can see that it’s very important to be able to create more than one WP_Post object.

The Global $wpdb Object

The $wpdb object is also global, and it’s also an object. So let’s get thinking: Is there ever a reason to have two $wpdb; objects?

The answer is a pretty resounding no! The $wpdb object is a wrapper for PHP access to the WordPress Database. In this case, the global object is actually less about storing data, and more about providing an API to access some external service. In this instance, a MySQL server.

The be all and end all, however, can be found in bold in the codex entry for $wpdb:

Warning: Methods in the wpdb() class should not be called directly. Use the global $wpdb object instead!

Pretty succinct: Don’t make one yourself, always use the global $wpdb.

This makes $wpdb a great candidate for being a Singleton, so, why isn’t it?

Probably to maintain backwards compatibility. Which is a good enough reason as any.

Ok, let’s use singletons sometimes.

Yes, let’s.

If you have a global data structure that you really don’t want developers duplicating. Whether it be because that data structure contains specific values that the developer might want. Or because it abstracts some service access behind an API, and having more than one would be nonsensical. Singletons are a great way or making sure you can never have two instances of the same class.

Next up, learn about how the two common ways of implementing singletons in: Singletons: Two ways about it.

You may also be interested in these articles

wordpress-saas

undefined | Reading time 2 min

WordPress for Digital Products (Saas)

wordpress-ehandel

undefined | Reading time 5 minutes

E-commerce

E-commerce is simply the service of selling and buying products and services via the internet.. I…

Read more
wordpress-ehandel

undefined | Reading time 5 min

E-commerce

wordpress-lms

undefined | Reading time 4 minutes

WordPress as an LMS

Do you have an idea for a perfect online course that will make participants flock without knowing…

Read more
wordpress-lms

undefined | Reading time 4 min

WordPress as an LMS

Subscribe to our newsletter for tips, inspiration and insight about WordPress and WooCommerce and the digital world beyond.

Subscribe to our newsletter and get lots of tips and inspiration!

Time to take the next step towards a more effective website?

Contact us, and we can talk more about how we can take your business to the next level together.