Interface Examples For Object Oriented PHP

Interface Examples For Object Oriented PHP

In this tutorial, we’ll take a look at Interface Examples while programming in Object Oriented PHP. Interfaces are one of the main building blocks of the SOLID pattern. When I first saw the word SOLID, as in someone referring to SOLID programming, I thought they meant it was solid, as in cool or legitimate. 🙂 LOL. Or course in reality, SOLID is the idea of the group of design patterns, conventions, and philosophies made popular by Uncle Bob Martin. These are the Single responsibility principle, Open Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion. We’ll cover all of these ideas moving forward. For now, let’s make sure the idea of an Interface is solidly cemented in our gray matter when object oriented programming.

What Is An Interface?

An interface is an outline of what a particular object can do. You will often hear that an interface is a contract. This is true in as much as an interface defines the public methods that a class must implement. For example, if you have two objects that are each instances of a different class, yet they implement the same interface, then both objects must at a minimum offer the public methods defined in the interface which they implement. An interface contains no logic. In addition to this, interfaces contain no data variables, only function prototypes.

Interface Syntax

When looking at the code, an interface almost looks like a class definition. Instead of using the class keyword however, we simply use the interface keyword when coding. In addition, the methods we define in the interface will have no curly braces {}, as there is no logic. Let’s see an example of an interface.

That’s it. Not too exciting is it? 🙂

Implementing an Interface

We implement an interface to enforce behavior. Consider that you are shopping for a new Lawnmower. You have narrowed it down to three models. One from Kubota, one from John Deere, and one from CrappyMowersInc. Two of these implement the Lawnmower interface, that is to say they are guaranteed to be able to cut_grass(). The third does not implement the Lawnmower interface. Let’s go lawnmower shopping in our code.

As we see here, the two models that implement the correct interface are the ones we like. In addition, since both of the classes we like implement our Lawnmower interface, they are essentially interchangeable. So it doesn’t really make a huge difference if you go with the Kubota or the JohnDeere, they will both cut your grass just fine. CrappyMowersInc on the other hand does not implement our Lawnmower interface, and therefore does not provide the result we want.

Program To an Interface

We now come upon another idea relating to interfaces, and that is to program to an interface, not an implementation. What it means is that if you have a class that will need to have multiple different ways to do the same thing, you can create an interface. Let’s imagine a landscaping company that is going to make use of our mowers. The company is going to need to cut the lawns of its customers. It doesn’t matter if they do it with a JohnDeere, a Kubota, a push mower, or a pair of scissors. As long as they get the job done right, the customer will be happy. First off, let’s create a landscaper class that makes use of a JohnDeere to cut lawns.

Things are going great. We have already helped two customers today and they are thrilled. Unfortunately, the blade on our JohnDeere hit a rock, and is now damaged. We don’t have any spare parts, so it looks like we’ll need to use our backup Kubota to finish the last job of the day. Let’s try that.

🙁 oh no! The Henrys are upset since we are unable to finish the job. The problem is, we passed in a concretion, or implementation, into the constructor of the Landscaping class. Instead, we should have programmed to an interface. This way, we can swap out the John Deere for a Kubota and it will work just fine. The reason it will work just fine is because both the JohnDeere class and the Kubota class implement the same interface. This means, they can complete the same exact behavior, which in this case is to cut_grass(). Let’s update our code to be more flexible. Check out this one small change we make to our Landscaper class.

Notice that we now pass in an interface into the constructor of the Landscaper class. Now when we new up that class later on in our client code, we can pass in a JohnDeere or a Kubota, and nobody squaks! The class no longer cares what you give it, or how it completes it’s job. As long as it can complete the job, which in this case is to cut grass, everything works. Don’t believe me? Let’s make the Landscaping crew cut the grass with a pair of scissors.

Programming to Concretion vs Programming to an Interface

To further illustrate how this works, lets look at this from the perspective of the Landscaper class. In our first example, we program to a concretion. We can see this, because we pass in a JohnDeere which is a concrete class (a real class with logic that does something). This means that the Landscaper class is saying to the world, “Hey, I need a JohnDeere to do my job. Don’t give me a Kubota, or a Craftsman, or a Honda, or a Pushmower, or a pair of scissors. If you give me anything other than a JohnDeere, I’m going to croak.”

Of course this is not the most flexible solution in the world. In the second example, we program to an interface. This means that we pass in an interface to the constructor instead of a concretion. This changes what the Landscaper class says to the world. It now says, “In order to do my job, you must provide me something that can cut_grass(). If you provide me something that is able to cut grass, I will do my job. I don’t care what you give me, just as long as it can cut grass.”

Interface Examples For Object Oriented PHP Summary

Interfaces provide many benefits to the programmer. They provide the ability for a developer to reference objects in a more abstract way. Interfaces also provide the ability to swap out implementations much more quickly and easily than if you had programmed to a concretion. Finally, the use of interfaces can be used to enforce contracts in our code. We can specify explicitly what behaviors, or methods, a class should have when it implements our interface. At the end of the day, pretty much the entire suite of SOLID principles relates to the use of an interface in one way or another, so it pays to know how interfaces work.