Object-Oriented Programming in WordPress: A Summary

Over the last 12 posts, we've taken a look at the basics of PHP, the basics of object-oriented programming, how to do so within the context of WordPress, and we've even looked at the beginning of more intermediate concepts such as inheritance.

At this point, it's time to draw this beginner's series to a close but prior to doing so, I'd like to provide a summary of each of the articles so that we not only have a refresher of the everything that we've done, but so that we also have a single summary page to bookmark for reference.

With that said, let's review everything we've covered up to this article. We'll include links back to the original article, short descriptions, and other pertinent information.

A Review of Object-Oriented Programming in WordPress

In the first post of the series, we discussed where we were headed with the articles that were to follow. In short, we provided a high-level outline as to what we'd be discussing, and then moved forward from there.

Perhaps the most important take away from this article was understanding "where do I start?" Which is a question many people ask when getting started with programming. 

To that, we said:

But those who have been at it for a significant amount of time often forget what it was like when originally trying to figure out how to decipher code, understand why something was written the way that it was, how the author knew to use what function and where, and determine the rationale behind certain implementation decisions.
We've all been there at some point, right? We've looked at the code, tried to figure out the flow of control, and at one time asked "where do I even start?" 
And the purpose of this series is to answer that question.

And so that's exactly what we aimed to do with the following points that were covered each in their own article.

1. Classes

The purpose of this post was to define the foundation of object-oriented programming - classes. First, we mentioned that classes are typically defined as the following:

A class is a blueprint for creating an object.

But we also recognized that this is a particularly confusing for most people especially if they're just beginning object-oriented programming. 

So instead, we talked about class in terms of what characteristics it defines:

So let's generalize this idea to objects. In fact, let's substitute one word for another:
A noun is an object.
An adjective an an attribute (or a property).
A verb is a method (or a function).

Additionally, we looked at both good and bad examples as to what defines a class, and we worked on defining a mental model for how to pictures classes when working with them

This ultimately laid the ground work for the plugin that we'd be writing in the future. But first, we needed to make sure that we had a firm understanding of the basics of PHP before moving into the more advanced feature of classes.

2. Types

In this article, we talked about the two types that exist within WordPress:

  1. Simple Types
  2. Complex Types

And then we defined each of the above as such:

Simple data types are defined as such because the data that they represent is, y'know, simple. That is to say that it will normally fall under the banner of true, false, decimals, and words and/or sentences.

And then we said:

The two primary complex datatypes that we're going to focus on in this series as arrays and objects. There are more, but they are outside the scope of this series, so if you're interested, then feel free to take a look at the PHP manual, but I warn you: if you're an absolute beginner, the content may feel a little overwhelming.

In short, examples of the above can be illustrated as:

  • booleans
  • integers
  • floating point numbers
  • strings
  • arrays
  • objects
  • ...and more

Of course, these are primarily useful once we start using them within the context of more advanced features such as conditional statements and control structures.

3. Control Structures

In the first article in the Control Structures series, we talked about conditional statements

First, recall that:

"Control Structures" is a fancy term term that describes how we can, ahem, control how the code flows through our program based a number of factors.

The two control structures that we talked about are if/then statements and switch/case statements, then we looked at examples of each. On top of that, we employed these in some of the code that we wrote in either our plugin or in our example of inheritance.

In the same series, we talked about loops.  Remember:

Assume that we have a set of data, perhaps a set of 10 posts, and that we want to loop through and print out the title and date of each post. Loops allow us to do this.

The list of loops at which we looked included:

  • for
  • foreach
  • do
  • while

And we looked at examples of each and how to use them while iterating through a variety of data structures.

4. Functions and Attributes

After covering some of the foundational aspects of PHP development, we moved on to covering functions - which can still be used in procedural programming - and attributes, which are unique to object-oriented programming.

To summarize, functions are used to complete a unit of work but they also use some of the aforementioned structures to help complete said work:

Variables, conditionals, loops, and so on are responsible for completing a single unit work, as well; however, each of those work in conjunction with one another to achieve something slightly greater than themselves.

We then took a look at an example of various functions - some which were extremely simply, others which were more complex that leveraged all of the above types, control structures, and loops.

But that's not all: Since functions can exist within a class and help a class complete their work, they also work in conjunction with attributes (which are the adjectives of an object, if you recall from earlier in the article).

The thing about attributes is this:

They are nothing but variables as we've looked at earlier in the series, and they can hold any type of value be it a primitive data type such as a string, integer, boolean or it can reference a more complex data type such as an array or another object.

The thing is, they aren't locked into a function. Instead, they live at the class level. And when they reside at the class level, there's a level of scope that they - along with the functions - must have.

5. Scope

From there, we began talking about scope.

In short, scope refers to how variables and functions can be access from third-party objects or child objects within the program. 

In the article, we even looked at a high-level diagram as well as some source code that demonstrated the point.

The key takeaway; however, is that scope can come in three different flavors:

  1. public which is available to the class itself and all third-party classes
  2. protected which is available to the class itself and all subclasses
  3. private which is available only to the class in which it is defined

This became even more clear as we began building our plugin using what we've learned.

6. Building The Plugin 

In the Building The Plugin series, we first talked about exactly what we'd actually be building and then we actually began to implement the plugin.

Throughout this process, we learned the importance of planning out the plugin before we actually begin implementation so that we have a roadmap, of sorts, in order to know where we're headed.

After doing that, we then began the actual implementation of the ideas that we had outlined to the point where we had a fully functional plugin that covered exactly everything we had covered up to this point.

In fact, we made the plugin available for download on GitHub.

But we weren't done yet. After that, we needed to document the plugin using proper code comments to explicate what each of our files, classes, attributes, and methods do.

7. Document the Plugin

In this series of articles, we first talked about the PSR standards as well as the WordPress Coding Standards and we began documenting the basics of our plugin.

However, it wasn't until the second part of the series that we really began to employ the documentation strategies as providing in the WordPress Coding Standards. In this post, we rounded out the rest of our documentation efforts by providing comments for every class, attribute, function, and even require statement that exists within the plugin.

Ultimately, this rounded out development of the plugin and allowed us to transfer our discussion to a more intermediate topic.

8. Inheritance

Over the next two posts, we covered one of the more intermediate topics of object-oriented programming: Inheritance. This wasn't meant to be an all inclusive-primer on the topic, but it was meant to be enough to help those of you with a budding interest in object-oriented programming in PHP become familiar with how it works.

In the first article, we took a look at some of the fundamentals as well as how it's used throughout the WordPress application specifically when using widgets.

In the final article, we built our own implementation of inheritance that, although very simplistic, provided a workable demonstration for how inheritance works within PHP.


Obviously, we have covered a lot of content in this series. Hopefully, those of you who are just getting started with working in WordPress and object-oriented programming in general have found it useful.

Though I'm not opposed to running another series of advanced topics, I'm more curious about your feedback, comments, and questions on the current series. Please feel free to offer that up in the comment feed.

As far as any future series are concerned, let me know and we'll see what we can do.

Other than that, good luck with your endeavors with WordPress, object-oriented programming, and so on. I can't wait to see what you come up with!



Related Articles