The Concepts of Object-Oriented PHP You Need to Understand for WordPress Development

objects-the-basis-of-object-oriented-programming
Tell us what you think of your hosting for the 2014 WordPress hosting survey!

Object-orientation has been the dominant paradigm in programming for more than a few decades now. And its dominance is so absolute that many non-programmers will be able to name it, but can’t name another programming paradigm. Many programmers and non-programmers alike make the mistake of thinking that object-orientation (sometimes OO, or OOP for “object-oriented programming”) is the way that professional programmers do it, and everything else is wrong or inferior.

But, for better or worse, this isn’t a think-piece about programming styles. This is a no-nonsense walk-thru of the basics of what you need to know about object-oriented programming to be more efficient when working with WordPress. Many of the more complicated programming things that a self-taught developer will hit in WordPress are the object-oriented bits — WP_Query and WP_Widget are the two I’m specifically thinking of. For me, they were literally hours of mind-bending time lost when I first encountered them because I didn’t understand the core concepts of OOP. So our goal today is to give you a basis upon which to get your head around these surprisingly powerful ideas.

A Basic Primer on the Value of Object-Based Thinking

By “abstracting” concepts into categories we all understand, we’re able to communicate efficiently and act effectively.

As thinking people, we reason mostly in terms of “objects” or “aggregates.” What I mean is that you’ll rarely consider the actions of Bob’s right thumb, you’ll think about Bob as an agent that’s moving his hands. Similarly you’ll rarely think about the various parts that make up the chair you’re sitting on, or the computer you’re using; they are objects that have properties that arise from their parts, but we typically relate to and reason about them as higher-level objects. If when we wanted to talk about a chair we had to exactly define human-brain-thinkingall the parts — even if only at the level of different materials — that went into it, we’d get very little accomplished. By “abstracting” concepts into categories we all understand, we’re able to communicate efficiently and act effectively. Imagine how insane life would be if you couldn’t do something as simple as ask Susan to “Please bring five more chairs” with her when she comes!

This basic abstracting of all the pieces and processes of a chair into its whole is the reason that object oriented programming exists. While it’s clearly useful to be able to use a computer or give ordered instructions to follow, as you start to try to make computers do more complicated things the inability to deal with things like “chairs” and “computers” and “people” becomes a real obstacle. When everything has to be an instruction at the lowest level of the computer, or even the language like PHP, your ability to reason and act quickly with the system is really diminished.

What are “Classes” and “Objects”?

So, the basic value of object oriented software design is that you get to think more like you typically do in the rest of your life. You get to have objects that perform tasks, rather having to do everything as a series of instructions that can take little for granted. The first place this gets translated into the hard language of PHP is in terms of classes.

We instruct the language about the “classes” of objects that it needs to know about, and when it gives us a single one — a chair — we call and think of that as an “object.”

If you’re new at this, the thought “I thought we were doing object-oriented programming” may come to mind. Well in PHP, and almost every other object-oriented language, the term for a concept like a chair, computer, or person is called a “class.” That is, we instruct the language about the classes of objects that it needs to know about, and when it gives us a single one — a chair, which will have all the properties we expect chairs to have, and do the jobs chairs do — we call and think of that as an “object.”

an-office-chair-object-oriented-exampleSo how do I ask PHP for an “instance” of a chair? I tell it a want a new one. So very naively, here’s the basic dynamic in code:

class Chair {
    public $color;
    private $elevation_method;

    public function raise_seat() {}
}
$object = new Chair;

So what does this do? It creates a new “class” or type of object, called “Chair” — in PHP and most other object-oriented languages, by convention you capitalize your classes. And in the last line, we’re making an “instance” of the class, which we’re storing in the variable $object which is a single chair.

What are Properties and Methods of Objects?

To flesh out the example a bit I added some details. Most chairs have a color (though some have many). Because practically we need and want the world to know the color of the chair we’ve made this a “public property.” That is it can be accessed and changed if we find it at $object->color in the PHP code like so:

$object->color = "green";

Worth keeping in mind: that changed the color of the specific chair, or “instance” of the Chair class, and not all chairs we’ll ever make. To do that, you’d want to change the code for the class itself.

Similarly, because the way you change the height of your chair —maybe it’s a pneumatic tube, maybe you put it on risers, maybe you just can’t do that — is something the chair needs to know but the world doesn’t, we’ve made this a private property. That means we can’t find it or change it on our object instance at $object->elevation_method, as we could with the color, but internally the chair can use it (by addressing it as $this->elevation_method).

private-keep-outThis private property idea is really useful as you start to move away from the theoretics of things like chairs and start to need to actually program real behavior into a User class, or a BlogPost class, which should have some private data accessible to all their “methods.”

One of the first roadblocks you may hit in moving from more simple PHP to more complex is terminology. What’s a property? What’s a method? Well, it’s pretty simple really, hopefully you caught on that “properties” are just a word we use for variable that are defined inside our class. And “methods”? Those are just the functions that travel along with your class. In our theoretical case above, the class has a single “method”, raise_seat(), which isn’t actually going to do anything because we didn’t tell it how to do anything.

We’ve Only Just Begun…

There’s a lot of nuance and detail that you can get into with respect to programming in general, and object-oriented programming in specific. What kinds of things should be public properties — accessible to the outside world — and which shouldn’t? What methods should be public, and which shouldn’t? Should someone be able to call a method of your class without having created an “instance” of it with the new keyword? Those and many other things are relevant as you start to hone your skill with this paradigm and really get better at it.

But, they aren’t really necessary for you to work effectively with the basic object-oriented APIs that WordPress provides for you. They’re vital to understand if you want to help WordPress change APIs and contribute to the core project, but they’re beyond what you really need to know to be effective doing things with WordPress. By all means, I’d encourage you to learn them. I’m an advocate of constant learning, but to avoid overwhelm we’ll stop here. Stay tuned as we start to dive into WP_Query, WP_Widget, and the other WordPress APIs that these little morsels of OOP will help you understand!

Image Credits: peregrine blue, _DJ_, bfionline, Nina Matthews

Found this helpful? You'll find us helpful!

WPShout is published by a full-service WordPress-loving constultancy, Press Up. Get in touch with us about your project or business idea, we're knowledgable and friendly.

About the author

David likes learning, solving hard problems, and teaching. He bikes a lot, and lives in (and loves) Colorado. You can find him on Twitter as @davidbhayes and check out everything else he does at davidbhayes.com.

5 Comments

 

Add a comment

required

required

optional