Learning PHP Through WordPress Development: Object Oriented Programming

Josh Pollock - October 13, 2014

Mandelbrot FractalMandelbrot Fractal

There are two basic types of programming, functional, IE based on functions, and object-oriented. If you’re learning PHP and programming in general by learning WordPress, as I have done, you probably started with functional programing. It’s how most themes are written, and its super simple.

Object-oriented PHP (OOP,) is a little tougher to learn, but it’s worth it. Learning OOP is one of the key steps in leveling up your programing skills.

This post is not a tutorial on learning OPP. Instead a collection of posts and series on learning object-oriented PHP for WordPress development, in the order I would recommend starting from a very basic introduction that I wrote and slowly getting more advanced while working through some articles by some of the finest WordPress tutorial writers out there.

Think it as the reading list for a course on OOP that I really should teach one day.

The Reading List:

Demystifying Objected Oriented PHP For WordPress Developers

I wrote this post for Torque as I very basic introduction to OOP. It uses WP_Query as one of its examples, since that’s one of the places where a lot of use first get introduced to it.

Organizing Your Files In An Object-Oriented WordPress Plugin

If you’re anything like me, you’re going to want to start putting all of this to use in a plugin right away. Before you do, I recommend thinking through how you’re going to organize the plugin itself and this post, from Carl Alexander, is a great one on the subject.

This series by Tom McFarlin covers the basics of using OOP to write a WordPress plugin.  Tom is one of my favorite writers on WordPress development and this series nails all of the basics.

Tuts+’s recent redesign makes it harder to read a lot their series in sequences. Luckily Tom has a list of links to all the posts in this on his site.

Read It! Use It!

I really got into OOP because working with Pods forced me to. If you haven’t, by this point started writing a plugin or put these concepts to use somehow start doing it now. Beyond the ability to use what you’re learning in your own work, you should now be able to read other people’s work better and learn from it. Find someone you respect, and read their code.

For example, I’ve never really used Easy Digital Downloads for anything yet, but I’ve read a lot of its source code. It’s well written and standards compliant. I’ve even borrowed some of it for some of my own work. One of the most important freedoms of the GPL is the ability to share with your neighbor, don’t squander that opportunity.

Carl Alexander’s Posts on OOP

Carl is quickly becoming one of my favorite WordPress tutorial writers. His posts have helped me up my game a lot. From his blog I learned about how to use class inheritance to reduce redundancy and a lot of other powerful, advanced topics. You should probably read everything he has written on OOP,  but a good staring list would be these two articles:

  • Designing A Class – Carl walks you through his thought process in designing a class. Bonus points for being REST API related.
  • Polymorphism and WordPress – Ever cut and pasted some code from one class into another or three, only to realize later that the basic pattern was wrong? I have, and last time that bit me in the butt, I turned to this article and its follow ups to learn how to stop making this mistake.