If you’ve read my post about classes in PHP, then you should have a fair understanding of how those work. I didn’t go into too much detail because I would like to build upon what you read previously. Hopefully, we can get a good foundation for you to continue to learn.

I was at our Beckley office today, telling a co-worker about my previous post, and what instantiation meant. Her knowing it comes from the word instance, helped her understand it’s definition and how I was using it. The word interface may sound like a difficult concept to grasp, but it isn’t. Below is a simple interface and how you would use it.

Everyone has to eat (at least I think they do) and some people get angry when they don’t eat. Eating, amongst other things, is required for a person to stay alive. In our interface, we defined a method (a function) called eat that accepts $food. We then change our Person class to implement the PersonInterface. Now the Person class MUST have an eat method.

Using interfaces may seem like an extra step, extra code, and a waste of time. I assure you they are not and I’ll do my best to explain why they are useful. Without the interface, you really won’t know what all the Person class will do. Now anytime you get a Person object, you can always call the eat method. When writing an extensive library, it can become easy to forgot to include some of the required methods for a class.

One class can implement multiple interfaces as shown below.

Now the Person class implements two interfaces and has more methods. The same rule applies with the BathroomInterface as did the PersonInterface, the Person class MUST have a poop, pee, and washHands method.

Below is an example of all of it put together and how you may use it.

An interface is an agreement that the class will have those methods. You can also type hint your parameters for a method by using the name of an interface. You could also type hint your parameter using the name of a class, but then you are forever locked in (I’m exaggerating) to using that class, which will get messy. Leaving our Person example below is an example of a type hinted method with an interface.

Try and walk through the above logic in your head and figure out what will happen if the TV was off. I hope this example shows how you can use an interface to predict the methods you will need to use. We know anything that implements the TechnologyInterface will have those two methods, we can call those functions without worrying if they will exist, because they must.

I don’t want to beat a dead horse, so I’ll stop here. Feel free to email me with any questions you may have or if you need further explanation.