PHP Interfaces

I’ve found as I progress in my career, there are many people I’ve spoken with that aren’t as familiar with the OOP side of PHP. Procedural coding in PHP is pretty easy to grasp for most, but true architects build with structure, which procedural coding quickly turns to mushy spaghetti quick. I plan to have a series of posts outlining some of the cool pieces of Object Oriented Programming found in PHP.

This first post we’ll talk about interfaces. Sure we could talk all about design patterns, but what I found most interesting is those less talked about pieces. While this isn’t really something that is new to PHP, in my line of work, I don’t find many others using it. Let’s talk about what an Interface is see what it can do to strengthen our OOP skills.

What is an Interface?

PHP interfaces are abstract classes that define a set of required methods, creating a “contract” (i.e. a blue print or template) that all other classes must implement. These methods contain no functional code inside of them and are left to be further then determined how they are used within the class that it is implemented on.

A great example of this would be an automobile. While each vehicle is unique in its own way, there are common components that all vehicle’s share. They have steering wheels, drive shafts, airbags, wheels; Combined, these make up a blue print that all vehicle’s MUST abide by, while defining their own unique touches to them. The steering wheel could have different adjustments, more airbags in the vehicle, alloy wheels, etc. This is where an interface is very helpful in OOP, a lead engineer could define the requirements early in the process that classes can then implement and make sure they are defining all the requirements.

Another great thing about interfaces is that we are not limited to the same restrictions of multiple inheritance. You may have run into the challenge of needing to extend multiple classes, which is not allowed with in PHP. Thankfully, we can implement multiple interfaces if we need to. Extending our Automobile interface discussion, we could create new interfaces for different types of vehicles that share other common characteristics like a truck, electric motor, limo, etc. From there, you could implement both the Automobile and a Truck interface to a single class.

Code Samples

Let’s see what this looks like in practice. We’ll use our basic Automobile interface example. We first define the interface class.

Now we would implement this interface to our main class. We’ll ignore the type interface we mentioned above and keep this a simple. We’ll create a Car A and Car B class.

Not the greatest example, but this should help bring this home a little. What’s most important is the process of how we implement an interface to a class and how our class methods are defined. The actual code written in the methods are unique to your class so the route I took to populating the $specs variable is not a requirement, this was just for example.

Hopefully this encourages you to find opportunities to use interfaces in your applications more often and create more flexibility! If you want to learn more around this, here’s some awesome content - happy learning!