If you use object-oriented programming, there’s a good chance that you’re familiar with the constructor. It’s this special method that an object-oriented programming language uses to initialize a new object. In PHP (and a lot of other object-oriented programming languages), the
__construct method is a special method reserved for the role of constructor.
The role of the constructor isn’t always well understood. This is especially true when you’re new to object-oriented programming. You know the method exists, but you’re not too sure how to use it.
This can lead to some less than ideal practices around constructors. This is especially true in the WordPress world where two such practices are popular. First, there’s the habit of putting hooks in the constructor. And, because of that habit, WordPress developers tend to overuse the singleton pattern.
These two practices show a misunderstanding of the role that a constructor plays inside a class. This isn’t how object-oriented programming wants you to use constructors. There are other ways to solve those problems without sacrificing the role of a constructor.
Now, this isn’t to say that constructors don’t have any problems either. They do! In fact, that’s why we’re looking at the static factory method pattern. It solves one common problem that constructors have in PHP.
Continue reading Using the static factory method pattern with WordPress
Let’s talk about teaching. While not the main topic discussed on this site, it’s still a topic comes up as a topic on a pretty regular basis. That’s because it’s an important and underrated way of growing as a developer.
As a WordPress developer, one way to teach that comes up over and over is speaking at a WordCamp. If you’re not familiar with WordCamps, they’re community-run WordPress conferences. There were 115 WordCamps in 41 countries in 2016 alone.
This means that the odds are pretty good that there’s a WordCamp near you! (Those odds are even better if you’re in North America or Europe.) So all that you have to do is apply and you’ll get in. That’s how it works right!?
Well, that’s not quite true. More and more people are applying each year. I’ve been a WordCamp organizer since 2011 and we saw a record number of submissions this year. (We got 130!)
This can make it hard for you to stand out from everyone else. You’re just one submission out of many. So what can you do to tip the odds in your favour?
Continue reading Tips for applying to a WordCamp
Most of us are familiar with how a web application works. We know that when we enter an URL in our browser, it’ll contact a web server who’ll return some HTML. The browser will then make follow-up requests and then render the web page. (This is a simplification! But that’s the main idea.)
But what’s happening on the web server returning HTML? How is the web server getting the HTML that it returns to our browser? And, to be even more specific, how does this happen in PHP?
This isn’t something that most of us know how to answer. We know (or at least should know!) how to build a PHP website. But what happens between our PHP code and the browser is a bit of a mystery.
There’s a good chance that you’re thinking, “Well, my site loads. Why should I think twice about this?” And that’s true! But the truth is that knowing how a PHP application works can be useful.
It can affect how you design your PHP application. But it’s also important if you’re looking to improve its performance. For example, it’s hard to understand PHP application caching if you don’t understand how a PHP application works.
Continue reading How does a PHP application work?
I spoke at WordCamp Halifax 2017 on how to troubleshoot the performance of a WordPress site. This is the companion article that I wrote for it. If you’re just looking for the slides, click here.
Have you ever had a client come up to you and ask why a WordPress page was slow to load? We’ve all had that happen at some point or another. Especially nowadays with the increased importance of page load times! (Everything has to be fast fast fast!)
But what do you do when someone asks you to fix such an issue? It can feel like you’re looking for a needle in a haystack. The issue could be anywhere. (It really can!)
So what can you do about it? Well, there are a lot of tools at your disposal. They can help you figure out what’s going on with the performance of your WordPress site.
That said, it’s one thing to know that these tools exist. It’s another one to be able to use them and interpret what they’re telling you. The problem is that it’s what you need to get to the root of a WordPress performance issue.
Continue reading How to troubleshoot WordPress performance
As WordPress developers, we often have to generate HTML content for our plugins. (And, if you’re building a theme, that’s pretty much mandatory!) But generating HTML inside a function or method is messy. You have to close your PHP tag, write your HTML, reopen your PHP tag and so on.
This combination of HTML and code isn’t ideal. You’ve coupled your templating to your code in a way that often doesn’t scale well. That’s why it’s common to see such functions or methods grow out of proportion.
The common cause of that is adding conditional logic into the HTML code. This increases the complexity of the code and reduces its readability. So we’d like to avoid that if possible.
It’s worth pointing out that mixing HTML and PHP isn’t bad per se. After all, PHP is also a templating language. We just want to create a proper delimitation between what is regular PHP code and what is PHP template code.
So let’s find a solution to this problem! The twist (well it’s not really a twist for this site…) is that we’re going to use object-oriented programming to do it. We’ll design a class that generates HTML using PHP template.
Continue reading Designing a class to generate HTML content in WordPress
As WordPress developers, we often need a way to store and retrieve data. The most common reason for that is that we need to store and retrieve settings for our plugin or theme. But the reality is that there are countless of other reasons why you’d need to store and retrieve data.
So how do we do it? Well, the primary tool that we have at our disposal to do this is the options API. It’s an API that offers a way for us to store and retrieve data from the WordPress database.
It’s pretty much as indispensable as the plugin API. We often can’t build anything in WordPress without it. That’s why it’s worth looking a bit deeper at how it works.
Continue reading WordPress for the adventurous: Options API
So let’s talk about coupling! Coupling is a complicated problem because you can never get rid of it completely. All that you can do is control how much it spreads. And there’s more than one way to do that.
One of these ways is the dependency inversion principle. It’s one of the most important principles in object-oriented design. And it’s why it’s part of the famous SOLID design principles. (It’s the “D” in SOLID.)
The dependency inversion principle isn’t something that you see with WordPress. That’s because most WordPress developers don’t use object-oriented programming. (But it’s always a good time to start if you’re not using it!) And the dependency inversion principle only helps when you’ve been using it for a while.
But what makes the dependency inversion principle special? Why is it such almost a mandatory aspect of object-oriented design? These are good questions that you deserve an answer to.
Continue reading Dependency inversion principle and WordPress
I gave an introduction to automated WordPress deployments at WordCamp Miami 2017. This is the companion article that I wrote for it. If you’re just looking for the slides, click here.
How do you feel when you have to update the code on a client’s WordPress site? Do you feel confident that everything will be ok? Or are you filled with dread as if you were about to play a round of Russian roulette?
For a lot of us, it’s a lot closer to the Russian roulette! We press the upload button of our favourite FTP client. We then spend the next minute refreshing the home page in our browser hoping not to see a white screen of death. (Followed by a sigh of relief when everything loads as it should!)
This is a pretty stressful way to work. You shouldn’t have to feel this way each time that you want to update the code of a WordPress site. It should be something that you can do as often as you want without worrying that you broke your site in the process.
And that’s where the idea of automated WordPress deployment comes in. As the name implies, it’s all about automating this process of updating a WordPress site. This, in turn, makes this risky process safer. (But keep in mind that this isn’t a bulletproof solution!)
Gone are the days where you’re playing Russian roulette each time that you want to make a change! You can now update your client’s WordPress site with a lot more confidence. This lets you focus on shipping bug fixes (yay bugs!) and new features.
That said, there are a lot of different ways of automating your WordPress deployments. We’ll go over what makes a successful automated WordPress deployment workflow. We’ll also look at a lot of different tools that you can use to achieve it.
Continue reading Introduction to automated WordPress deployments
I spoke at WordCamp Atlanta 2017 on how you could use teaching as a learning tool. This is the companion article that I wrote for it. If you’re just looking for the slides, click here.
As WordPress developers (or developers in general), we’re always looking to improve our skills. We read blog posts, watch screencasts, listen to talks at WordCamps and so on. These are all great ways to learn new things.
But there’s one way to hone your programming skills that doesn’t get as much attention as it should. It’s “learning by teaching“. Teaching is a great way to improve your skills as a developer.
Now, you might be wondering, “Seriously, how can teaching make me a better developer?” People don’t tend to associate teaching with skill building. After all, how could you teach something if you didn’t know it already?
That’s a good question! It does feel like this shouldn’t be able to work. But it does! And that’s why it’s worth taking a better look at this idea of “learning by teaching”.
Continue reading How to use teaching as a learning tool
So this is another article that I’m doing for myself and also to be transparent. I don’t change my computer often (My old computer was a mid-2011 MacBook Air!) and I usually use the opportunity to review my workflow. This time around I wanted to document the process in some way.
The initial plan was to do something like this. That said, a few people told me that it would be something useful to write down. So this is what this article is going to be about.
Since everything is still quite fresh, I can walk through my thought process. I think it’s useful when you’re trying to make decisions whether this tool is useful for you or not. We don’t all work the same way so my workflow might not fit yours.
It’s also worth pointing out that this is going to be macOS centric. I haven’t developed on another operating system in a long time. So you might not get as much out of this if you’re using Windows or Linux.
Alright, so you have your new MacBook Pro box in front of you! You’re ready to get going. You unbox it, boot it up, set up your account for the first time and get to the desktop screen. What’s next!?
Continue reading How I setup my 2016 MacBook Pro