Wednesday , 26 July 2017
Home / Programming / PHP / Interfaces – PHP OOP Tutorial
Interfaces
Interfaces

Interfaces – PHP OOP Tutorial

This tutorial is a part of the PHP OOP tutorial series. If you would like to view the complete list of tutorials under this series and the order you should follow, then click here. http://jaskokoyn.com/php-oop-tutorial-series/ 

In this tutorial, you’ll learn how to create interfaces and why you should use them. They are very similar to abstract classes. At the end of this tutorial, we’ll discuss the differences and the benefits of each one.

We’ll be creating something similar to our recipe and cookie class in our abstraction tutorial.

Creating Interfaces

Create a file called index.php and add this bit of code.

Create a folder called interfaces and then create the recipe.interface.php file and cookie.interface.php file.  Let’s first create the Recipe interface.

Here’s what our Recipe interface looks like.

To create an interface you use the interface keyword. This keyword tells PHP that you’re creating an interface. An interface can not contain properties, but it can contain constants and methods. Any method you create in the interface can not contain any concrete code inside of it. All methods created must be public. You’ll also notice we don’t have to repeat the interface keyword before every method.

Now, that we’ve created an interface. Let’s create the cookie class.

Inside the cookie.interface.php file, add this bit of code.

You’ll notice that we’re not using the keyword extends to use the interface. Instead, you have to use the implements keyword in order to use an interface. Just like abstract classes, you must define any methods defined in the interface in your class.

Just like abstract classes, you CAN NOT instantiate interfaces. So, in our index.php file, we instantiate the cookie class. This is how you create interfaces.

Interfaces can be useful as they can be used as an empty shell or template for classes that need logic structured for them.

Interfaces vs Abstract Classes

Interfaces and abstract classes are pretty similar. They both help create templates for classes that need them. So, which one do you use and what are the major differences? Here a couple of things to know about each one.

  1. Methods in abstract classes can be defined abstract or just a regular method. If a method is abstract, then that method must be defined in the class that extends it. In interfaces, ALL methods are abstract, therefore all methods must be defined in the class that implements them.
  2. You’re allowed to define your methods as public, private, or protected in abstract classes. In interfaces, ALL methods MUST be public.
  3. In abstract classes, you’re allowed to create methods and properties with values and concrete code in them as long as they’re not defined as abstract. Interfaces CAN NOT contain properties and methods CAN NOT have a body.
  4. A class can only extend 1 abstract class, but a class can implement an infinite amount of interfaces even if the interfaces don’t relate to one another.
  5. You have the option to override methods in an abstract class, but you MUST override ALL methods in an interface.

As I stated earlier, you can have a class inherit multiple interfaces. To do this, you separate each interface with a comma. Let’s say I had an interface named business. Here’s how our cookie class would implement both the recipe and business interface.

Conclusion

As you can see, interfaces are a bit more stricter than abstract classes. Interfaces are meant to be used  just as a template or empty shell for other classes to follow a certain logic. Abstract classes are lenient and can contain some helpful methods and properties for any class that extends it. It’s up to you to choose which one best suits your project. In most cases, you won’t find yourself using interfaces or abstract classes often. For more information on interfaces, then go here.

http://php.net/manual/en/language.oop5.interfaces.php

About Jasko Koyn

Check Also

Static Methods and Properties

Static Methods and Properties – PHP OOP Tutorial

Let’s learn how to create static methods and properties. Anything that’s static is basically something …

5 comments

  1. You wrote “You’re allowed to define your methods as public, private, or protected in abstract classes” but Private method shouldn’t be abstract since we can’t implement it in child classes
  2. nice tutorial I was confused on when to use an interface or an abstract class but you made ur pretty clear. but which of them is most secure
  3. wonderful information about Interfaces vs abstract class.

    Please share your knowledge with us;
    thanks in advance.


    Kranthi Kumar P.

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link