Want to get more articles like this one? Join my newsletter

The first thing you should learn from object-oriented programming

You’ve decided to learn object-oriented programming, but you don’t know where to start. Object-oriented programming has so many concepts and features. The whole thing can feel overwhelming at times. Let’s help you get started on the right foot.

What’s a great place to start? With the feature that you’ll associate the most with object-oriented programming. It’s called encapsulation. Most modern programming languages support encapsulation using classes.

Why should you learn about it first?

If you don’t know about classes, encapsulation is a great way to learn about them. In theory, you put your code in a class and you’re done! It’s magic! The problem is that it’s hard to see the value of doing that. That’s because encapsulation is the cornerstone of the larger object-oriented programming puzzle. All the other features more or less depend on it, but, by itself, it doesn’t do much.

What’s encapsulation?

Encapsulation is all about information stored inside your object. It’s about controlling access to it. That way others can’t tamper with it. Instead, you use class methods to interact with it. The result is that your code is stronger and less complex.

Why is it important?

When you set your oven to 400 degrees, you expect the displayed temperature to be accurate. You don’t want to know how the oven got the information or how it processed it. You just want the ability to set and view your oven’s temperature.

That’s what encapsulation wants to do as well. When you use a class, you expect a consistent behaviour. You expect the information inside to be accurate and untampered with. How the object handles the information internally shouldn’t be important to you. It’s the result you get when you use it that counts.

As a programmer, it easier to code an object if you can control how the object handles its internal information. By hiding internal object information, you’re sure no one played with it. The only way to keep it safe is to use the methods that you made accessible to others. You limit problems down the road since there’s a specific way to use your object.

How classes support encapsulation

PHP classes have three features to support encapsulation.

Properties

Variables that are specific to a class are called properties. They are used to store information inside an object.

Methods

Functions that are specific to a class are called methods. These methods have access to the class properties. By using methods, you can control the interaction with those properties.

Visibility

Visibility is the most important feature that classes offer. It is how a class controls the access to its properties and methods. There are three possible access levels:

  • Public
  • Private
  • Protected

“Public” gives access to a property or method from anywhere. “Private” only gives access to the property or method in the class that defined it. “Protected” is almost the same as private. The difference is that it gives access to the property or method in the inherited and parent classes as well.

The difference between “private” and “protected” isn’t important when discussing encapsulation. This distinction becomes relevant when you start using inheritance. However, that topic will need its own article. At this point, you can view them as the same.

In PHP, setting the visibility of a property or method is optional. By default, PHP will set the visibility to “public”.

What it looks like in practice

Previously, we created the WP_Filters class as an example. The goal of the class was to encapsulate all the WordPress hooks and filters functionality.

class WP_Filters
{
    protected $filters = array();

    /**
     * Hooks a function or method to a specific filter action.
     *
     * @param string  $tag
     * @param mixed   $function
     * @param integer $priority
     * @param integer $accepted_args
     */
    public function add($tag, $function, $priority = 10, $accepted_args = 1)
    {
    }

    /**
     * Call the functions added to a filter hook.
     *
     * @param string $tag
     * @param mixed $value
     */
    public function apply($tag, $value)
    {
    }

    /**
     * Remove all of the hooks from a filter.
     * 
     * @param string  $tag
     */
    public function clear($tag)
    {
    }

    /**
     * Removes a function from a specified filter hook.
     *
     * @param string  $tag
     * @param mixed   $function
     * @param integer $priority
     */
    public function remove($tag, $function, $priority = 10)
    {
    }

    /**
     * Build Unique ID for storage and retrieval.
     *
     * @param string  $tag
     * @param mixed   $function
     * @param integer $priority
     */
    protected function build_unique_id($tag, $function, $priority)
    {
    }
}

You can see all the elements of encapsulation at play in the example. The protected variable $filters isn’t accessible outside the class. You need to use the public methods to interact with it.

The class uses the method build_unique_id internally. That is why it’s marked as protected. From the outside, it’s not important how the class creates a unique id. There’s no need to use the method outside the class either.

It’s only the beginning

Encapsulation makes your code stronger, but it’s only a first step. By combining it with other object-oriented features, you’ll be able to do even greater things.

That’s when object-oriented programming starts to shine.

Creative Commons License