In the previous two posts (available here and here), I talked a bit about the Singleton Design Pattern, dependency injection, and dependency injection containers.
Not that kind of container (but it’s still a cool shot).
These are all topics that I think are important for developers to know and to understand. If you’ve not read the previous posts, then I recommend it because the code that’s shown later in this post assumes you know a little bit about each of the topics mentioned above.
Furthermore, this is going to be a bit shorter as it relates to the previous two posts. The purpose is simply to show how to use a singleton as a simple dependency injection container.
In the previous post, I said that I was going to talk about how to use the Singleton Design Pattern as a simplistic way to introduce a dependency injection container into a project.
One of the best comments I’ve seen about dependency injection containers comes from Stack Overflow (and Joel Spolsky, even):
IoC containers take a simple, elegant, and useful concept, and make it something you have to study for two days with a 200-page manual.
And there’s a time and a post for where I could digress, but that’s not this post. Instead, there are a few nuances about this idea that I want to clarify before I go any further:
Dependency Injection Containers are more than just ways to store objects. They handle other additional logic. I’ll cover more about this later in the article.
I don’t recommend sticking with an implementation of the Singleton Design Pattern for a container (or for very many things, for that matter).
The purpose of showing this as a strategy is a way to show how you can take a project with a tight deadline, a desire to use software development best practices, and find some practical middle ground.
All of that to say is that what I’m going to show is not what I consider being a best practice for using dependency injection containers.
Instead, it’s a way to “meet in the middle” when it comes to working under pressure for building solutions for others all the while not wanting to sacrifice sound engineering principles.
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.
If you’ve done any work with object-oriented programming and familiar design patterns, then it’s likely that you’ve come across it (if you haven’t used it).
Depending on who you ask, the singleton design pattern may be treated as an anti-pattern, like some weird use of a “poor man’s namespace,” or one of the many other negative views of it.
Though these perspectives aren’t necessarily wrong, there are times where it’s okay to use it.
Dependency injection allows a program design to follow the dependency inversion principle. The client delegates to external code (the injector) the responsibility of providing its dependencies.
The client is not allowed to call the injector code. It is the injecting code that constructs the services and calls the client to inject them. This means the client code does not need to know about the injecting code.
But think about it like this (because if you’ve used object-oriented programming, you’ve no doubt done something like this):
You have a class, say Class A that maintains a reference to another class, Class B.
In the constructor for Class A, we instantiate Class B and set it equal to a property.
Easy enough, right? Here’s the implication:
Class A now has a dependency on Class B and that dependency is only set during the instantiation process in the constructor of Class A.
On a small scale, this isn’t that big of a deal but as a plugin or an application gets more and more complicated, there are all of these dependencies set in the system without any way to necessarily test them in isolation.
And don’t get me wrong: There should be somecohesion among objects in an application, but the degree to which they are coupled should be small. There are a lot of reasons for this many of which are outside the scope of this post.
To help mitigate this, programmers have created all kinds of strategies to make sure that our classes can be small and focused and tested in isolation all the while working with other parts of the system.
And one of the most popular ways to do this is through dependency injection. But what does dependency injection in WordPress look like (and is it any different than in other applications)?
WordPress hooks are arguably the foundational topic that developers need to learn and understand when it comes to building more advanced functionality for WordPress.
Yes, you can build an entire theme primarily with markup, styles, and JavaScript. Though you may need a few server-side functions in PHP, it’s easy to find things like this in the WordPress Codex or in Underscores.me.
But if you’re someone who’s new to WordPress or who is ready to take the next step regarding extending the platform or writing a plugin, then it’s important to understand to know what WordPress hooks are, how they work, and what they’re designed to do.
And that’s what I’ve covered in my latest series on Envato.
Kinsta offers premium managed WordPress hosting for everyone, small or large. Powered by Google Platform and strengthened by Cloudflare, they take performance to the next level. All hosting plans include 24/7/365 support from their team of WordPress engineers. Get startedwith a free migration today.
If you are looking for WordPress guides and unbiased reviews, I recommend checking out WPKube. They also have an exclusive deals section.
Just Getting Started with WordPress? I write a lot about WordPress development but if you're just getting started, I recommend checking out WPBeginner. They have free training videos, glossary, and more.